2017-09-17 9 views
0

페이징 기능이있는 scrollview가 있습니다. 이 스크롤 뷰에서는 동적으로 xib를로드합니다. 그러나 xib의 높이는 스크롤보기와 같아야합니다. 내 문제는 xib의 높이가 올바르게로드되지 않았다는 것입니다.UIScrollView에서 XIB가 제대로로드되지 않았습니다.

높이가 너무 커서 xib 파일 (각 경계선을 제외하고는 거의 전체 화면이 10 인 제약 조건)을로드하면 제대로로드되지 않습니다. 큰.

enter image description here

누구든지 해결책을 가지고 : 내있는 ScrollView에 맞도록

그래서 아래의 제약을해야?

+0

어떤 Xcode 버전을 사용하고 있습니까? Apple은'Xcode 9 '에서 Auto Layout과 많은 관련이있었습니다. 질문을 업데이트 할 수 있다면 도움이 될 것입니다. – KSigWyatt

+0

콘텐츠 뷰를 스크롤 뷰와 동일한 높이로 설정하는 제약 조건을 추가해보십시오. – RPK

답변

1

아래 코드로 실험 해보십시오. 이 일반적인 접근 방식은 내 프로젝트에서 작동합니다. 문제는 height 속성에 대한 모호한 정의입니다. .xib에서 불필요한 제약 조건을 찾는데주의하십시오.

@IBOutlet weak var yourCustomScrollView: UIScrollView! 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 

    // Firstly, define scrollview's position and size 
    yourCustomScrollView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height) 

    // Load .xib with custom class from main bundle.   
    guard let xib = Bundle.main.loadNibNamed("YourXibName", owner: self, options: nil)?.first as? YourCustomXibClass else { 
     fatalError("YourXibName is not found. ") 
    } 
    self.yourCustomScrollView.addSubView(xib) 

    // Define .xib's position and size 
    xib.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height) 
    yourCustomScrollView.contentSize = CGSize(width: self.view.frame.width, height: xib.frame.height) 
} 
+0

덕분에 도움이되었습니다! – LJulz