UIView子类在控制台中绑定不正确 [英] UIView subclass bounds incorrect in console
问题描述
这里我创建了名为CustomSlider的UIView子类。 CustomSlider在Storyboard上添加并与storyboard outlet参考相关联。当我打印边界时,我遇到打印边界的问题,它提供了错误的控制台输出。
Here i have created UIView subclass named as CustomSlider. CustomSlider added on Storyboard and connected with storyboard outlet reference. When i am printing the bounds, I am having issues with printing the bounds, its providing incorrect console output.
class CustomSlider: UIView {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override init(frame: CGRect) {
super.init(frame: frame)
self.setupView()
}
func setupView() {
print("self.bounds.height \(self.bounds)" )
self.backgroundColor = UIColor.red
}
}
class ViewController: UIViewController {
@IBOutlet weak var slider: CustomSlider?
override func viewDidLoad() {
super.viewDidLoad()
slider?.setupView()
}
}
当我尝试构建输出界限时显示如下:
When i am trying to build the output bounds displaying as below :
self.bounds.height (0.0, 0.0, 1000.0, 1000.0)
<但是我设置了尾随,顶部,右侧,高度的约束。我也试过下面的行。
But i'm setting constraints trailing, top, right, height. I have tried below line also.
slider?.translatesAutoresizingMaskIntoConstraints = false
这里是我项目的截图
here is screenshot my project looks like
我预期的控制台输出将是滑块边界。
My expected console output would be the slider bounds.
推荐答案
打印界限
的电话来得太早了。从xib / storyboard加载的视图具有虚假边界(如图所示),直到发生 layout 。布局在 viewDidLoad
时发生 not ;视图尚未在界面中,没有尺寸尚未实现。推迟访问 bounds
,直到至少在第一次调用 viewDidLayoutSubviews
之后。
Your call to print the bounds
comes too soon. A view being loaded from a xib/storyboard has bogus bounds (as shown) until layout has occurred. Layout has not occurred at the time of viewDidLoad
; the view is not yet in the interface and no sizes are real yet. Postpone your access to bounds
until at least after viewDidLayoutSubviews
has been called for the first time.
这篇关于UIView子类在控制台中绑定不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!