【问题标题】:How to integrate safeAreaInsets to current constraints extension如何将 safeAreaInsets 集成到当前约束扩展
【发布时间】:2017-09-26 09:39:05
【问题描述】:

我为视图的约束创建了扩展。您可以在下面找到代码,但是在新发布的 iPhone X 之后,我们需要使用 safeAreaInsets 但我不知道如何实现该属性。如果你能帮助我,我会很高兴。

谢谢

    extension UIView {

    func anchor (top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?,  paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat) {
        translatesAutoresizingMaskIntoConstraints = false

        if let top = top {
            self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
        }
        if let left = left {
            self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
        }
        if let right = right {
            rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
        }
        if let bottom = bottom {
            bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true
        }
        if height != 0 {
            heightAnchor.constraint(equalToConstant: height).isActive = true
        }
        if width != 0 {
            widthAnchor.constraint(equalToConstant: width).isActive = true
        }

    }

}

【问题讨论】:

    标签: ios iphone swift ios11 safearealayoutguide


    【解决方案1】:

    我已重构您的代码以考虑插入。请尝试以下操作:

    import UIKit
    
    extension UIView {
    
        func anchor (top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?,  paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat, enableInsets: Bool) {
            var topInset = CGFloat(0)
            var bottomInset = CGFloat(0)
            var rightInset = CGFloat(0)
            var leftInset = CGFloat(0)
    
            if #available(iOS 11, *), enableInsets {
              let insets = self.safeAreaInsets
              topInset = insets.top
              bottomInset = insets.bottom
              rightInset = insets.right
              leftInset = insets.left
            }
    
            translatesAutoresizingMaskIntoConstraints = false
    
            if let top = top {
                self.topAnchor.constraint(equalTo: top, constant: paddingTop+topInset).isActive = true
            }
            if let left = left {
                self.leftAnchor.constraint(equalTo: left, constant: paddingLeft+leftInset).isActive = true
            }
            if let right = right {
                rightAnchor.constraint(equalTo: right, constant: -paddingRight-rightInset).isActive = true
            }
            if let bottom = bottom {
                bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom-bottomInset).isActive = true
            }
            if height != 0 {
                heightAnchor.constraint(equalToConstant: height).isActive = true
            }
            if width != 0 {
                widthAnchor.constraint(equalToConstant: width).isActive = true
            }
    
        }
    
    }
    

    【讨论】:

    • 我尝试实现您编写的代码,但它不起作用 topInset 值总是返回 0.0
    • 你是在你的vc的根视图上调用扩展方法吗?
    • 是的,我一步一步检查,我还为 topInset 值结果编写了打印函数,例如 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value是:0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0 值是: 0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0 值是:0.0
    • 我想我发现了问题,我在 viewdidload 方法中使用这些视图后我改变了它 viewDidLayoutSubviews 它工作我想我会尝试
    • 这可能会给其他元素带来混乱,比如如果我想从上部元素填充 5 个顶部,它将给出 5 + topInset.value
    猜你喜欢
    • 2018-05-09
    • 2013-12-26
    • 2023-03-14
    • 2020-02-19
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多