gpt4 book ai didi

ios - 如何让我的 HeightAnchor 正常工作?

转载 作者:行者123 更新时间:2023-11-30 11:06:12 28 4
gpt4 key购买 nike

在我的 MainVC 中,我试图将 UIView 限制在顶部、左侧、右侧,并且高度为 80。现在,我的 View 是全屏的。我如何修复我的代码以获得正确的大小?

//变量 var topViewCons : [NSLayoutConstraint] = []

// Constants
let topGradient = RadialGradientLayer()
let topMainView = UIView()
// MainVC Top View Constraints
topMainView.translatesAutoresizingMaskIntoConstraints = false
topGradient.frame = view.bounds
topMainView.layer.addSublayer(topGradient)
self.view.addSubview(topMainView)

let topConstraint = topMainView.topAnchor.constraint(equalTo: self.view.topAnchor)
let leftConstraint = topMainView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor)
let rightConstraint = topMainView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
let topViewHeight = topMainView.heightAnchor.constraint(equalToConstant: 80)
NSLayoutConstraint.activate([topConstraint, leftConstraint, rightConstraint, topViewHeight])

最佳答案

不是全屏,渐变的是

topGradient.frame = view.bounds  // here you make it's frame to screen bounds 
topMainView.layer.addSublayer(topGradient)

所以你需要设置

topMainView.clipsToBounds = true

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
topGradient.frame = topMainView.bounds
}
<小时/>

您也可以直接执行此操作,无需让

NSLayoutConstraint.activate([
topMainView.topAnchor.constraint(equalTo: self.view.topAnchor),
topMainView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
topMainView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
topMainView.heightAnchor.constraint(equalToConstant: 80)
])

关于ios - 如何让我的 HeightAnchor 正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52692983/

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