ios - 自动布局错误

标签 ios objective-c uibutton autolayout

我遇到了与 this poster 类似的问题.并且我使用了 jrturton 的建议将自定义按钮的代码移动到 viewDidLayoutSubviews 中。在我收到此错误之前,它一直运行良好:

'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.'

我对图形一无所知,我唯一能想到的就是放置 [self.view layoutSubviews]; 但这并没有解决任何问题。当我取消选中 Storyboard 中的“自动布局”时它起作用了,但这改变了我的按钮的尺寸,我想知道是否有另一种方法来修复它?

代码:

-(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];
    }
}

最佳答案

我最近遇到了这个问题,当时我想通过提供 viewDidLayoutSubviews 消息处理程序以编程方式调整 Storyboard创建的 View 的框架。按照异常消息的说明,我尝试在 -viewDidLayoutSubviews 的末尾添加 [self.view layoutIfNeeded],然后成功了。

关于ios - 自动布局错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15490140/

相关文章:

ios - 如何获取 iOS 13 中的状态栏高度?

ios - 我有一个 Objective-C iPad 应用程序,该应用程序发出如下失败的 Web 服务调用 :

ios - 需要释放包含在 CALayer 的内容属性中的 CGImage

xcode - 在 swift4 中制作圆形按钮

ios - UITableView tableHeaderView 中的 UIButton 不响应触摸

ios - 如何将 NSObject 数组存储在 Core Data 中?

iphone - 尝试使用 SQLite 包装器 FMDatabase 时出错

ios - 将状态栏置于 View 之下

objective-c - 当子类化 NSCoder 时,我是否还需要子类化集合类(即 :NSArray, NSDictionary)?

ios - swift 3 : How to use action button in multiple sections of a table view?