自动布局错误 [英] Auto Layout error

查看:126
本文介绍了自动布局错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也有类似的问题<一href=\"http://stackoverflow.com/questions/13368197/cant-do-custom-uibutton-in-ios6-with-enabled-storyboard-autolayout\">this海报。我用jrturton的建议移动code自定义的按钮到 viewDidLayoutSubviews 。这是工作顺利,直到我收到此错误:


  

NSInternalInconsistencyException',原因是:自动布局仍
  发送-viewDidLayoutSubviews到视图控制器后需要。
  的ViewController的实现需要发送-layoutSubviews到
  查看调用自动布局。


我对图形很无能,我能想到的唯一的事情就是把 [self.view layoutSubviews]; 但没有任何修复。它工作时,我在故事板未选中自动布​​局,但是这改变了我的按钮的尺寸,我想知道是否有另一种方式来解决它?

code:

   - (无效){viewDidLayoutSubviews
    NSArray的* arrayOfButtons = [NSArray的arrayWithObjects:self.decimalButton,self.buttonOne,self.buttonTwo,self.buttonThree,零]    对于(UIButton的*在arrayOfButtons按钮){        [按钮setTitleColor:[的UIColor whiteColor] forState:UIControlStateNormal];
        [按钮setTitleColor:[的UIColor redColor] forState:UIControlStateHighlighted];        button.layer.borderWidth = 0.25f;
        button.layer.borderColor = [[的UIColor grayColor] CGColor];        CAGradientLayer * btnGradient = [CAGradientLayer层]
        btnGradient.frame = button.bounds;
        btnGradient.colors = [NSArray的arrayWithObjects:
                              (ID)[的UIColor colorWithRed:122.0f / 255.0f绿色:188.0f / 255.0f蓝:255.0f / 255.0f阿尔法:1.0F] CGColor]
                              (ID)[的UIColor colorWithRed:96.0f / 255.0f绿色:171.0f / 255.0f蓝:248.0f / 255.0f阿尔法:1.0F] CGColor]
                              零];
        [button.layer insertSublayer:btnGradient atIndex:0];
    }
}


解决方案

最近我有这个问题,当时我就想,通过提供编程调整由故事板创建的视图帧 viewDidLayoutSubviews 消息处理程序。继异常消息的指令,我试图添加 [self.view layoutIfNeeded] -viewDidLayoutSubviews 结束后,然后它的工作。

I had a similar problem to this poster. And I used jrturton's suggestion to move the code for customizing the buttons into viewDidLayoutSubviews. It was working well until I received this error:

'NSInternalInconsistencyException', reason: 'Auto Layout still required after sending -viewDidLayoutSubviews to the view controller. ViewController's implementation needs to send -layoutSubviews to the view to invoke auto layout.'

I'm very clueless on graphics, and the only thing I could think of was to put [self.view layoutSubviews]; but that didn't fix anything. It worked when I unchecked "Auto Layout" in my Storyboard, but that changed the dimensions of my buttons, and I was wondering if there was another way to fix it?

Code:

-(void)viewDidLayoutSubviews {
    NSArray *arrayOfButtons = [NSArray arrayWithObjects:self.decimalButton, self.buttonOne, self.buttonTwo, self.buttonThree, nil];

    for (UIButton *button in arrayOfButtons) {

        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

        button.layer.borderWidth = 0.25f;
        button.layer.borderColor = [[UIColor grayColor] CGColor];

        CAGradientLayer *btnGradient = [CAGradientLayer layer];
        btnGradient.frame = button.bounds;
        btnGradient.colors = [NSArray arrayWithObjects:
                              (id)[[UIColor colorWithRed:122.0f / 255.0f green:188.0f / 255.0f blue:255.0f / 255.0f alpha:1.0f] CGColor],
                              (id)[[UIColor colorWithRed:96.0f / 255.0f green:171.0f / 255.0f blue:248.0f / 255.0f alpha:1.0f] CGColor],
                              nil];
        [button.layer insertSublayer:btnGradient atIndex:0];
    }
}

解决方案

I had this issue recently, when I wanted to programmatically adjust frame of the view created by storyboard, by providing a viewDidLayoutSubviews message handler. Following the instruction from exception message, I tried to add [self.view layoutIfNeeded] at the end of -viewDidLayoutSubviews, and then it worked.

这篇关于自动布局错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆