reason: 'Cannot modify constraints for UINavigationBar managed by a controller'

我尝试给navigationBar的子控件设置约束

    UIView *menuView = [[UIView alloc] init];
    [self.navigationController.navigationBar addSubview:menuView];
    self.menuView = menuView;
    menuView.backgroundColor = [UIColor redColor];
    [menuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.centerY.mas_equalTo(self.navigationController.navigationBar);
    }];

但是当我运行程序奔溃:reason: 'Cannot modify constraints for UINavigationBar managed by a controller'

原因:自动布局只能使用在UIViews及其子类,比如它允许自定义导航栏的外观相关的属性,但动态地使用导航项目对象(UINavigationItem的情况下,您不会直接改变它的框架,边界或Alpha值...导航控制器建立导航栏的内容类)与导航堆栈上的视图控制器关联

你可能感兴趣的:(reason: 'Cannot modify constraints for UINavigationBar managed by a controller')