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

来源:互联网 发布:类似iebook的软件 编辑:程序博客网 时间:2024/06/05 02:51

当在UITabBarController底部TabBar自定义的时候,我自定义了一个View,然后添加到系统自带的tabBar上。因为当APP打开时,我要任意切换横屏和竖屏,所以我要让自定义的tabBar适配屏幕。


我首先想到的方法是用masonry来做约束:

- (void)setupTabbar{


    WWTabBar *customTabBar = [[WWTabBaralloc]init];

    customTabBar.backgroundColor = [UIColorwhiteColor];

    customTabBar.delegate =self;

    [self.tabBaraddSubview:customTabBar];

    self.customTabBar = customTabBar;

    

    [customTabBar mas_makeConstraints:^(MASConstraintMaker *make) {

        

        make.top.equalTo(self.tabBar.mas_top);

        make.left.equalTo(self.tabBar.mas_left);

        make.bottom.equalTo(self.tabBar.mas_bottom);

        make.right.equalTo(self.tabBar.mas_right);

    }];

    

}


结果是程序刚一启动,就崩溃了,跑出错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot modify constraints for UITabBar managed by a controller'


那么,为什么会出现这种错误呢?

因为​Autolayout只能对uiview和它的子类起作用。UIBarButtonItem不继承自UIView,Autolayout对UIBarButtonItem不起作用。


解决办法:

第一种方法:

- (void)setupTabbar{

    

    WWTabBar *customTabBar = [[WWTabBaralloc]init];

    customTabBar.backgroundColor = [UIColorwhiteColor];

    customTabBar.delegate =self;

    customTabBar.frame =self.tabBar.bounds;

    [self.tabBaraddSubview:customTabBar];

    self.customTabBar = customTabBar;

    

    customTabBar.autoresizingMask =UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;

}


第二种办法:在tabbar里加这个通知,横屏时重新布局一下(不建议)
 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeRotate:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];

- (void)changeRotate:(NSNotification*)noti {
    if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait
        || [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) {
       
        NSLog(@"竖屏");

    } else {
        
        NSLog(@"横屏");
    }
}


好了,到这就就说完了,小伙伴们赶快试试吧。


阅读全文
0 0
原创粉丝点击