我有一个项目,它的大部分布局逻辑都在IB中。我希望动态更新一些定位,所以我把它放在viewDidAppear:
中- (void)viewDidAppear:(BOOL)animated{
// Fill array of button tools
buttonArray = [[NSArray alloc] initWithObjects:
self.filterButton, self.bulgeButton, self.stickerButton, self.textButton, self.drawButton, self.invertedBulgeButton, nil];
// Position button tools
int const X_POSITION = 175; // The x position of the tool buttons
int yPosition = 0; // The x position of the tool buttons
CGRect frame = CGRectMake(X_POSITION, yPosition, 100, 100); // The position and size of the tool buttons
// Loop through buttons and set properties
for (UIButton *button in buttonArray) {
frame.origin.y = yPosition;
button.frame = frame;
yPosition += 75;
button.hidden = false;
}
}
我遇到的问题是,每当我按下另一个按钮时,我在viewDidAppear中应用的所有格式都将被撤消,按钮定位将更改为它们在IB中的位置。如何防止我的按钮移动到IB中设置的位置?
编辑:以下是一些会导致定位发生变化的代码:
// Undoes most recent edit to photo
- (IBAction)undoButtonPress:(id)sender {
[self undoImage];
}
- (void)undoImage {
// TODO: Add all the other current actions.... none of them work because they need to be checked for here
// Remove tools for the current action
self.imageSticker.image = nil;
[self refreshImageText];
currentAction = NONE;
self.mainImage.image = [_images pop];
}
// after done editing the text, this resets the text fields so you can create a second text
- (void) refreshImageText{
self.hiddenTextField.text = @"";
self.imageText.text = @"";
self.imageText.transform = CGAffineTransformMakeRotation(0);
[self.imageText setCenter:self.view.center];
}
答案 0 :(得分:1)
您需要更新约束,而不是对象的实际帧。因此,如果您将按钮框y约束添加为IBOutlet
,则可以轻松编辑它。
这是一个非常酷的工具,可以使用autolayout约束: https://autolayoutconstraints.com/
答案 1 :(得分:1)
我发现AutoLayout正在弄乱我的定位。我能够将translatesAutoresizingMaskIntoConstraints设置为true以防止AutoLayout弄乱我的设置:
// Loop through buttons and set properties
for (UIButton *button in buttonArray) {
frame.origin.y = yPosition;
button.frame = frame;
yPosition += 75;
button.hidden = false;
button.translatesAutoresizingMaskIntoConstraints = YES;
}