0
添加約束環路而在循環問題使用「addConstraint」上的UIButton與按鈕x位置來了,如何使用目標C
for (int ix = 0; ix<7; ix++) {
UIButton *segmentButton = [[UIButton alloc] init];
[segmentButton setTitle:[_segmentButtonTitleArray objectAtIndex:ix] forState:UIControlStateNormal];
[_segmentView addSubview:segmentButton];
[segmentButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[_segmentView addConstraint:[NSLayoutConstraint constraintWithItem:segmentButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:_segmentView
attribute:NSLayoutAttributeWidth
multiplier:1.0/7
constant:0]];
[_segmentView addConstraint:[NSLayoutConstraint constraintWithItem:segmentButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:segmentButton.frame.size.height]];
[_segmentView addConstraint:[NSLayoutConstraint constraintWithItem:segmentButton
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:_segmentView
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:segmentButtonXposition]];
[_segmentView addConstraint:[NSLayoutConstraint constraintWithItem:segmentButton
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:_segmentView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:segmentButton.layer.frame.origin.y]];
segmentButtonXposition = segmentButtonXposition +1+_segmentView.frame.size.width/7-1;
}
一個期望的結果是這樣,但無法產生結果。
你會得到什麼結果?你可以發佈正在製作的圖片嗎? –
hi @GavinHope已經發布了圖片http://i.stack.imgur.com/bewjn.png。加載後獲取結果。但是在旋轉之後,它的寬度爲 –
哦,所以初始加載會產生你想要的結果,但是如果你旋轉設備,你會得到錯誤的寬度?具體來說,如果從** portrait **旋轉到** landscape **,橫向是否保留縱向使用的* width *? –