why the autolayout is not updated if I use IBDesignable file when i run the app?

梦想与她 提交于 2019-12-04 17:54:29

The problem here is that you specify the frame height in setHeight(), but also specify the height with a layout constraint inside storyboard. One way to fix this is to specify an intrinsicContentSize for your view and not specify the height as a layout constraint in storyboard. (I.o.w. similar to the way you rely on the width of the label to be calculated for you)

@IBDesignable
class CustomParentNavigationBarView: UIView {

    lazy var deviceHasTopNotch: Bool = {
        if #available(iOS 11.0, tvOS 11.0, *) {
            // with notch: 44.0 on iPhone X, XS, XS Max, XR.
            // without notch: 20.0 on iPhone 8 on iOS 12+.
            return UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0 > 20
        }
        return false
    }()

    override var intrinsicContentSize: CGSize {
        let height:CGFloat = deviceHasTopNotch ? 88 : 64
        let width = super.intrinsicContentSize.width //Use whatever you prefer here.  Ex.UIScreen.main.bounds.size.width
        return CGSize(width: width, height: height)
    }
}

In Storyboard

Remove your current height constraint for the custom view. Set the Intrinsic Size property to Placeholder.

Example of the layout constraints for the custom nav bar.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!