将UIKit元素添加到子视图时,setFrame不起作用

时间:2014-11-24 11:20:25

标签: ios objective-c autolayout frame

我有看法,如下所示: view three

当我尝试在 CGSizeZero 框架中折叠 schedinaContView 时,仅当schedina的子视图为空时才有效。

这是我的代码:

-(void)prepareFloatSchedina
{
    schedinaVC = [[SchedinaViewController alloc] initWithNibName:@"SchedinaViewController" bundle:nil];
    schedinaVC.view.frame = self.schedina.frame;
    [self.schedina addSubview: schedinaVC.view];

    schedinaExpandedSize = _schedinaContView.frame.size;
    CGRect frame = [UIUtilCollection frameInSuperFrame:self.view.frame atCornerWithSize:schedinaCompressedSize];
    _schedinaContView.frame = frame;

    NSLog(NSStringFromCGRect(_schedinaContView.frame));

    UIButton *bottone = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)];
    [bottone setTitle:@"Ciao" forState:UIControlStateNormal];
    [self.schedina addSubview:bottone];
}

这是NSLog的结果:

  

{{1004,748},{0,0}}

但是_schedinaContView仍然存在于视图中!

所以,如果我删除UIButton,它完全有效。那是为什么?

解决:

我将方法[translatesAutoresizingMaskIntoConstraints:]的参数设置为YES 然后我在焦点视图上停用了NSLayoutConstraint,当我尝试打开视图时,我重新激活了。

[_schedinaContView setTranslatesAutoresizingMaskIntoConstraints:YES];
_verticalSpace.active = NO;

1 个答案:

答案 0 :(得分:2)

我看到自动布局限制。 setFrame不适用于autolayout。 使用约束代替setFrame或关闭自动布局。