【问题标题】:XIB isn't loaded properly in UIScrollView在 UIScrollView 中未正确加载 XIB
【发布时间】:2017-09-17 10:44:37
【问题描述】:

我有一个带分页的滚动视图。在这个滚动视图中,我动态加载 xibs。但是xib的高度应该和滚动视图一样。我的问题是xib的高度没有正确加载:

如果我加载一个 xib 文件(几乎是全屏的,除了每个边框上的约束为 10)它没有正确加载,因为高度太大。

所以我必须对底部进行约束,使其适合我的滚动视图:

有人有解决办法吗?

【问题讨论】:

  • 您使用的是哪个版本的 Xcode? Apple 在Xcode 9 中对自动布局进行了很多更改。如果您也可以用它来更新您的问题,可能会有所帮助。
  • 尝试添加一个约束,将您的内容视图设置为与滚动视图相同的高度。

标签: ios swift xcode uiscrollview


【解决方案1】:

尝试使用下面的代码。这种通用方法适用于我的项目。您的问题是高度属性的模糊定义。注意在你的 .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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-10
    • 2018-07-27
    • 2012-09-24
    • 1970-01-01
    • 2013-05-04
    • 2012-12-28
    • 1970-01-01
    • 2014-12-19
    相关资源
    最近更新 更多