gpt4 book ai didi

ios - NSLayoutAttribute topMargin 和 BottomMargin (边距约束)在 iOS 编程布局中不起作用

转载 作者:行者123 更新时间:2023-11-30 11:48:32 26 4
gpt4 key购买 nike

我正在通过编程限制使用 swift 4.0 开发 iOS 应用程序。但在 iOS 模拟器上运行时,mainView.topMargin 与 mainView.top 相同。我预计 mainView.topMargin 和 mainView.top 之间有 20 像素的差异。如果有人有任何想法,请帮忙。

我已经在下面发布了我的代码。请检查。

    // my scroll view
private let svLogin: UIScrollView = {
let scroll = UIScrollView(frame: .zero)
scroll.backgroundColor = UIColor.yellow//(hex: Colors.PRIMARY_DARK, alpha: 0)
scroll.delaysContentTouches = false
scroll.showsVerticalScrollIndicator = false
scroll.showsHorizontalScrollIndicator = false
scroll.isScrollEnabled = true
scroll.isPagingEnabled = false
scroll.bounces = false
scroll.isUserInteractionEnabled = true
scroll.clipsToBounds = true
scroll.translatesAutoresizingMaskIntoConstraints = false
return scroll
}()

// This is how I am applying constraints
view.layout(svLogin, .leading, .equal, view, .leading)
view.layout(svLogin, .top, .equal, view, .topMargin)
view.layout(svLogin, .width, .equal, view, .width)
view.layout(svLogin, .bottom, .equal, view, .bottomMargin)

// extension to create programmatic constraints

extension UIView {

@discardableResult
func layout(
_ leftItem: Any,
_ leftAttribute: NSLayoutAttribute,
_ relation: NSLayoutRelation,
_ rightItem: Any,
_ rightAttribute: NSLayoutAttribute,
_ multiplier: CGFloat,
_ constant: CGFloat
) -> NSLayoutConstraint {
if let leftView = leftItem as? UIView {
leftView.translatesAutoresizingMaskIntoConstraints = false
}

let constraint = NSLayoutConstraint(
item: leftItem,
attribute: leftAttribute,
relatedBy: relation,
toItem: rightItem,
attribute: rightAttribute,
multiplier: multiplier,
constant: constant
)
self.addConstraint(constraint)
return constraint
}

@discardableResult
func layout(
_ leftItem: Any,
_ leftAttribute: NSLayoutAttribute,
_ relation: NSLayoutRelation,
_ rightItem: Any,
_ rightAttribute: NSLayoutAttribute
) -> NSLayoutConstraint {
if let leftView = leftItem as? UIView {
leftView.translatesAutoresizingMaskIntoConstraints = false
}

let constraint = NSLayoutConstraint(
item: leftItem,
attribute: leftAttribute,
relatedBy: relation,
toItem: rightItem,
attribute: rightAttribute,
multiplier: 1,
constant: 0
)
self.addConstraint(constraint)
return constraint
}

@discardableResult
func layout(
_ leftItem: Any,
_ leftAttribute: NSLayoutAttribute,
_ relation: NSLayoutRelation,
_ constant: CGFloat
) -> NSLayoutConstraint {
if let leftView = leftItem as? UIView {
leftView.translatesAutoresizingMaskIntoConstraints = false
}

let constraint = NSLayoutConstraint(
item: leftItem,
attribute: leftAttribute,
relatedBy: relation,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: constant
)
self.addConstraint(constraint)
return constraint
}
}

最佳答案

我通过为 iOS 11+、iOS 9+、iOS 8+ 编写单独的逻辑解决了我的问题

if #available(iOS 11.0, *) {
svUploadWorks.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
svUploadWorks.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
svUploadWorks.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor).isActive = true
svUploadWorks.heightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.heightAnchor).isActive = true
} else {
if #available(iOS 9.0, *) {
svUploadWorks.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
svUploadWorks.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
svUploadWorks.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
svUploadWorks.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor).isActive = true
} else {
view.layout(svUploadWorks, .leading, .equal, view, .leading)
view.layout(svUploadWorks, .top, .equal, topLayoutGuide, .bottom)
view.layout(svUploadWorks, .width, .equal, view, .width)
view.layout(svUploadWorks, .bottom, .equal, bottomLayoutGuide, .top)
}
}

关于ios - NSLayoutAttribute topMargin 和 BottomMargin (边距约束)在 iOS 编程布局中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48556755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com