gpt4 book ai didi

iOS, subview 大小无故改变

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

 @objc class SearchViewController: UIViewController {

var uiView: UIView!

@IBOutlet weak var reactWrapperView: UIView!

override func viewDidLoad() {
super.viewDidLoad()


self.view.layer.borderColor = UIColor.yellow.cgColor
self.view.layer.borderWidth = 2

self.reactWrapperView.frame = (self.reactWrapperView.superview?.bounds)!
self.reactWrapperView.layer.borderColor = UIColor.red.cgColor
self.reactWrapperView.layer.borderWidth = 5

self.uiView = UIView(frame: (self.reactWrapperView.superview?.bounds)!)
self.uiView.layer.borderColor = UIColor.blue.cgColor
self.uiView.layer.borderWidth = 10

self.reactWrapperView.addSubview(self.uiView)

}
}

self.view 和 self.rectWrapperView 在 Storyboard中创建,它们的大小与手机屏幕相同,如下图所示。

self.view : yellow
self.reactWrapperView : red
self.uiView: blue

如您所见,底部蓝色边框未显示,并且 uiView 似乎变大了。

我已经挣扎了很长时间,想知道什么会影响大小。

enter image description here

最佳答案

您的Viewcontroller UI未更新(意味着未完全加载),您可以移入主线程或将代码移至viewwillappearviewDidAppear强>,原因 ui 没有更新。

选项1

override func viewDidLoad() {
super.viewDidLoad()

DispatchQueue.main.async { // or use sync


self.view.layer.borderColor = UIColor.yellow.cgColor
self.view.layer.borderWidth = 2

self.reactWrapperView.frame = (self.reactWrapperView.superview?.bounds)!
self.reactWrapperView.layer.borderColor = UIColor.red.cgColor
self.reactWrapperView.layer.borderWidth = 5

self.uiView = UIView(frame: (self.reactWrapperView.superview?.bounds)!)
self.uiView.layer.borderColor = UIColor.blue.cgColor
self.uiView.layer.borderWidth = 10

self.reactWrapperView.addSubview(self.uiView)

}
}
}

选项2

 override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)


self.view.layer.borderColor = UIColor.yellow.cgColor
self.view.layer.borderWidth = 2

self.reactWrapperView.frame = (self.reactWrapperView.superview?.bounds)!
self.reactWrapperView.layer.borderColor = UIColor.red.cgColor
self.reactWrapperView.layer.borderWidth = 5

self.uiView = UIView(frame: (self.reactWrapperView.superview?.bounds)!)
self.uiView.layer.borderColor = UIColor.blue.cgColor
self.uiView.layer.borderWidth = 10

self.reactWrapperView.addSubview(self.uiView)

}
}

关于iOS, subview 大小无故改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45955544/

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