iOS:UIButton Autolayout大小为零(iOS: UIButton Autolayout size is zero)
使用autolayout我想了解为什么会发生以下情况:创建一个简单的UIButton并设置其标题。 但是,即使我希望设置视图的内在内容大小并因此正确地获得其框架 - 按钮的框架显示(0,0,0,0)。 我需要做什么才能获得按钮的帧值?
-(void)viewDidLoad { [super viewDidLoad]; UIButton *button = [[UIButton alloc] init]; [button.titleLabel setText:@"My button"]; //button frame is zero }
然而,如果我这样做,那么事情就像预期的那样工作:
-(void)viewDidLoad { [super viewDidLoad]; UILabel *label = [[UILabel alloc] init]; [label setText:@"My label"]; [label sizeToFit]; //label frame has the expected width and height }
Using autolayout I want to understand why the following happens: Creating a simple UIButton and setting its title. However, even though I expect the intrinsic content size of the view to be set and hence get its frame properly - the frame of the button is showing (0, 0, 0, 0). What do I have to do to get the frame value of the button ?
-(void)viewDidLoad { [super viewDidLoad]; UIButton *button = [[UIButton alloc] init]; [button.titleLabel setText:@"My button"]; //button frame is zero }
Yet if I am to do that, then things work as expected:
-(void)viewDidLoad { [super viewDidLoad]; UILabel *label = [[UILabel alloc] init]; [label setText:@"My label"]; [label sizeToFit]; //label frame has the expected width and height }
原文:
更新时间:2022-11-17 10:11