【问题标题】:Custom XIB missing when loading with Swift 3 Xcode 8使用 Swift 3 Xcode 8 加载时缺少自定义 XIB
【发布时间】:2016-09-20 23:57:02
【问题描述】:

我正在使用带有自定义 XIB 的情节提要,在升级到 swift 2 和 xcode 7 之前一切正常。升级后可以将情节提要转换为 xcode 8。我的视图控制器显示视图,但缺少我的自定义 XIB .查看情节提要文件,使用自定义 XIB 转换情节提要似乎存在错误。其他人有这个问题吗?

required public init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: self.dynamicTypeName, bundle: bundle)
    let nibView = nib.instantiate(withOwner: self, options: nil).first as! UIView
    self.mainView = nibView
    self.mainView.frame = bounds
    self.mainView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    mainView.translatesAutoresizingMaskIntoConstraints = true
    self.addSubview(mainView)
}

【问题讨论】:

    标签: ios iphone xcode swift3 xcode8


    【解决方案1】:

    swift 3 的布局过程出了点问题,因为在升级之前

    调用 self.mainView.frame = bounds,会给你正确的边界,但现在使用 swift 3 它总是(0、0、1000、1000),所以我必须在 layoutSubviews 中设置框架以获得正确的边界.

    open override func layoutSubviews() {
        self.mainView.frame = bounds
    }
    

    【讨论】:

    • 我也遇到了同样的问题,你找到解决办法了吗。
    • 是的,尝试在 layoutSubviews 中设置您的自定义 xib 框架
    • 不知道为什么它没有自动取帧,尽管提供的房间仍然是全屏的(比如 mainscreen.rect)
    猜你喜欢
    • 2017-04-15
    • 2017-09-12
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 2017-01-20
    • 2015-04-10
    • 2020-05-16
    • 2017-10-01
    相关资源
    最近更新 更多