UIView 框架自动重置
UIView frame auto reset
我有这个简单的代码,初始化一个 UILabel
第一次打印时,frame width/height 是正确的,但第二次打印时,frame 的所有值都是 0。
let messageBoxFrame = CGRect(x: 0, y: 0, width: 1024, height: BAND_HEIGHT)
print(messageBoxFrame)
let messageBox = UILabel(frame: messageBoxFrame)
messageBox.textAlignment = .center
messageBox.textColor = UIColor.white
messageBox.backgroundColor = UIColor.blue
messageBox.sizeToFit()
print(messageBox.frame)
messageBox.sizeToFit()
这行代码正在调整您的 UILabel
框架
根据这个sizeToFit()
Call this method when you want to resize the current view so that it
uses the most appropriate amount of space. Specific UIKit views resize
themselves according to their own internal needs. In some cases, if a
view does not have a superview, it may size itself to the screen
bounds. Thus, if you want a given view to size itself to its parent
view, you should add it to the parent view before calling this method.
我有这个简单的代码,初始化一个 UILabel
第一次打印时,frame width/height 是正确的,但第二次打印时,frame 的所有值都是 0。
let messageBoxFrame = CGRect(x: 0, y: 0, width: 1024, height: BAND_HEIGHT)
print(messageBoxFrame)
let messageBox = UILabel(frame: messageBoxFrame)
messageBox.textAlignment = .center
messageBox.textColor = UIColor.white
messageBox.backgroundColor = UIColor.blue
messageBox.sizeToFit()
print(messageBox.frame)
messageBox.sizeToFit()
这行代码正在调整您的 UILabel
框架
根据这个sizeToFit()
Call this method when you want to resize the current view so that it uses the most appropriate amount of space. Specific UIKit views resize themselves according to their own internal needs. In some cases, if a view does not have a superview, it may size itself to the screen bounds. Thus, if you want a given view to size itself to its parent view, you should add it to the parent view before calling this method.