我有類似的問題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];
}
}
怎麼樣調用'[super viewDidLayoutSubviews]'?文檔說默認實現什麼都不做,但值得一試。 – 2013-03-19 04:02:37