gpt4 book ai didi

ios - Xcode 和 Swift。宽度是自动计算的,但在 iPhone Xr 上一切正常,在 iPhone X 上看起来很糟糕

转载 作者:行者123 更新时间:2023-12-03 20:50:31 24 4
gpt4 key购买 nike

在 Storyboard中:

in storyboard

在代码中,添加另一个 View 。我将新 View 放置在蓝色 View 中并将其居中

override func viewDidLoad() {
super.viewDidLoad()

print(viewTest.frame.width)

let newView = UIView(frame: CGRect(x: 0, y: 0, width: 380, height: 100))
newView.backgroundColor = .red

let x = viewTest.frame.width / 2 - newView.frame.width / 2
let y = viewTest.frame.height / 2 - newView.frame.height / 2

newView.frame = CGRect(x: x, y: y, width: newView.frame.width, height: newView.frame.height)

viewTest.addSubview(newView)

}

在 iPhone Xr 上看起来不错 iPhone Xr

iPhone X问题上,观点超越 iPhone X

最佳答案

由于屏幕尺寸不同,所以更换

let newView = UIView(frame: CGRect(x: 0, y: 0, width: 380, height: 100))

let newView = UIView(frame: CGRect(x: 0, y: 0, width:view.frame.width - 40.0, height: 100))
<小时/>

最好

newView.translatesAutoresizingMaskIntoConstraints = false
viewTest.addSubview(newView)
NSLayoutConstraint.activate([
newView.centerYAnchor.constraint(equalTo:viewTest.centerYAnchor),
newView.rightAnchor.constraint(equalTo:viewTest.rightAnchor,constant:-10),
newView.leftAnchor.constraint(equalTo: viewTest.leftAnchor,constant:10),
newView.heightAnchor.constraint(equalToConstant:100)
])

要更正 View 的访问框架,它应该位于 viewDidLayoutSubviews 内,而不是 viewDidLoad

关于ios - Xcode 和 Swift。宽度是自动计算的,但在 iPhone Xr 上一切正常,在 iPhone X 上看起来很糟糕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59646284/

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