添加topLayoutGuide后无法更改视图的框架

时间:2013-12-09 02:02:59

标签: ios autolayout

我在导航控制器视图的超级视图中添加了一个topLayoutGuide约束,然后当我尝试更改导航控制器视图的帧时,例如:从当前帧(0,0,320,568)到(200, 0,320,568)使用

[[navigationController view] setFrame:CGRectMake(200, 0, 320, 568)];

动画后它会自动设置为(0,0,320,568)。

此代码添加了topLayoutGuide约束:

- (void)viewDidLoad { // of subclassed UINavigationController class .m file
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) {
        [[self view] setTranslatesAutoresizingMaskIntoConstraints:NO];

        id topGuide = [self topLayoutGuide];
        UIView * selfView = [self view];
        NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (selfView, topGuide);
        [[[self view] superview] addConstraints:
         [NSLayoutConstraint constraintsWithVisualFormat:@"V:[topGuide]-0-[selfView]"
                                             options:0
                                             metrics:nil
                                               views:viewsDictionary]];
        [[[self view] superview] layoutSubviews]; // Must call this method here or the system raises an exception
    }
}

1 个答案:

答案 0 :(得分:1)

Autolayout努力使所有约束都得到满足。动画结束后,此约束将被破坏,在下一个布局循环中,它将满足您的约束并更改帧。 考虑动画布局约束更改或关闭自动布局。