iOS自定义UIView在iPhone XR和iPhone 7中具有不同的布局

时间:2019-06-11 20:23:09

标签: ios swift uiview autolayout

我遇到了自定义UIView的自动布局问题,该问题可以在iPhone XR中正确呈现

vi

但在iPhone 7s中出错(“重置”按钮超出范围) enter image description here

并在情节提要中仅使用viewcontroller(UI)进行设置,而没有指定UIViewController类,为简化起见,我只想调试视图,因此不将任何viewcontroller类与其关联。

p.s安全区域已使用

enter image description here

和自定义类UI(FilterView)设置: enter image description here 以及自定义类UI(FilterView)代码:

override init(frame: CGRect){
    super.init(frame: frame)
    setUpView()
}
//for IB
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setUpView()
}



private func setUpView(){
    Bundle.main.loadNibNamed(Constants.NIB_FILTER_VIEW_NAME, owner: self, options: nil)
    addSubview(contentView)
    contentView.frame = self.bounds
    contentView.autoresizingMask = []
    contentView.translatesAutoresizingMaskIntoConstraints = true

}

有人知道这里发生了什么吗?

1 个答案:

答案 0 :(得分:0)

您不应以此方式创建X按钮和“排序依据”标签。您正在执行的操作是复制UINavigationController的操作。如果需要这种布局,则将视图控制器放在UINavigationController中,这将使添加X和“ Sort by”标签更加容易。

此外,您还可以添加一个rightBarItem作为“重置”按钮。同样,您不必进行布局。 UINavigationController为您完成所有这些工作。