簡體   English   中英

自動布局錯誤

[英]Auto Layout error

我對這張海報有類似的問題。 我使用jrturton的建議將用於自定義按鈕的代碼移動到viewDidLayoutSubviews 它一直運行良好,直到我收到此錯誤:

'NSInternalInconsistencyException',原因:'將-viewDidLayoutSubviews發送到視圖控制器后仍需要自動布局。 ViewController的實現需要將-layoutSubviews發送到視圖以調用自動布局。

我對圖形非常無能,我唯一能想到的就是把[self.view layoutSubviews]; 但這並沒有解決任何問題。 它在我的故事板中取消選中“自動布局”時有效,但這改變了我按鈕的尺寸,我想知道是否還有其他方法可以修復它?

碼:

-(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創建的視圖框架。 按照異常消息的指示,我嘗試在-viewDidLayoutSubviews的末尾添加[self.view layoutIfNeeded] ,然后它就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM