繁体   English   中英

即使可见,UIView的宽度和高度也为零

[英]UIView has zero width and height even though it's visible

我创建了UIView的子类,并将其称为MSMobileControlView 我在启用Autolayout的情节提要中添加了UIView对象,并将其类分配给MSMobileControlView

MSMobileControlView内部,我有以下代码:

-(void)didMoveToSuperview
{
    NSLog(@"self.frame: %@", NSStringFromCGRect(self.frame));

    UIBUtton *levelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [levelButton setFrame:CGRectMake(0, 0, 150, 40)];
    [levelButton setTitle:@"Next Level" forState:UIControlStateNormal];
    [self addSubview:levelButton];
}

NSLog输出为:

self.frame: {{0, 0}, {0, 0}}

但是,我仍然可以在屏幕上看到视图,并且按钮可以按预期响应点击。 我没有与MSMobileControlView关联的其他代码。 到底是怎么回事?

Auto Layout运行时将在以后为框架分配值。 如果要查看那些分配的值,请将NSLog放在viewDidLayoutSubviews

如果我在开始使用“自动布局”时会提出一些建议,那就是忘了帧。 只需约束即可。 让“自动布局”代表您操纵框架以为您提供约束指定的布局。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM