【问题标题】:Adding constrain to UIView to bottom of UIView for iPhone has notch将 UIView 的约束添加到 iPhone 的 UIView 底部有缺口
【发布时间】:2021-02-11 08:19:29
【问题描述】:

如何使用约束将 UIView 添加到 UIView 的底部

    bottomView = UIView(frame:CGRect(x:0, y:leftMenuView.frame.height - 64, width: leftMenuView.frame.size.width, height:64))

    bottomView.translatesAutoresizingMaskIntoConstraints = false
    leftMenuView.addSubview(bottomView)

func setUpBottonViewConstrain(){

    NSLayoutConstraint.activate([

        bottomView.leadingAnchor.constraint(equalTo: self.leftMenuView.safeAreaLayoutGuide.leadingAnchor),
        bottomView.trailingAnchor.constraint(equalTo: self.leftMenuView.safeAreaLayoutGuide.trailingAnchor),
        bottomView.topAnchor.constraint(equalTo: self.leftMenuView.safeAreaLayoutGuide.bottomAnchor,constant:-64),
        bottomView.bottomAnchor.constraint(equalTo: self.leftMenuView.safeAreaLayoutGuide.bottomAnchor),
        ])
}

在 iPhone 5s 中工作正常,但在 iPhone12 中没有固定到底部的缺口

【问题讨论】:

  • 使用 bottomView.bottomAnchor.constraint(equalTo: self.leftMenuView.bottomAnchor) remove safeAreaLayoutGuide 假设您的 leftView 固定在屏幕底部,它将固定到底部,否则也对 leftMenuView 应用类似的约束

标签: swift uiview autolayout constraints


【解决方案1】:
func setUpBottonViewConstrain(){
    guard let superview = superview else { return }
    NSLayoutConstraint.activate([
        bottomView.leadingAnchor.constraint(equalTo: self.leftMenuView.safeAreaLayoutGuide.leadingAnchor),
        bottomView.trailingAnchor.constraint(equalTo: self.leftMenuView.safeAreaLayoutGuide.trailingAnchor),
        bottomView.topAnchor.constraint(equalTo: self.leftMenuView.safeAreaLayoutGuide.bottomAnchor,constant:-64),
        bottomView.bottomAnchor.constraint(equalTo: superview.bottomAnchor),
    ])
}

【讨论】:

    猜你喜欢
    • 2013-08-25
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 2013-01-13
    • 1970-01-01
    • 2016-02-13
    • 2014-08-23
    相关资源
    最近更新 更多