【发布时间】:2012-10-17 01:04:01
【问题描述】:
所以我试图在点击后移动UIButton。
点击按钮后调用_addMoreFields方法。
_addMoreFieldBtn 是一个全局 UIButton。当我单击它时,没有任何反应。
奇怪的是,如果我注释掉addSubView 代码,那么按钮就会移动。
如果我保留该代码,则按钮不会移动。
有什么想法吗?
-(void)movePlusButton {
NSLog(@"Moving button");
[UIButton beginAnimations:nil context:nil];
[UIButton setAnimationDuration:0.3];
_addMoreFieldsBtn.center = CGPointMake(30,30);
[UIButton commitAnimations];
}
- (IBAction)addMoreFields:(id)sender {
CGRect currentBtnFrame = [(UIButton *)sender frame];
CGPoint org = currentBtnFrame.origin;
UILabel *whoWasIn = [[UILabel alloc] initWithFrame:CGRectMake(110, org.y, 85, 21)];
whoWasIn.text = @"test";
UITextField *whoWasInField = [[UITextField alloc] initWithFrame:CGRectMake(59, whoWasIn.frame.origin.y+40, 202, 30)];
whoWasInField.placeholder = @"test2";
UILabel *with = [[UILabel alloc] initWithFrame:CGRectMake(136, whoWasInField.frame.origin.y+40, 49, 21)];
with.text = @"with";
whoWasInField.borderStyle = UITextBorderStyleRoundedRect;
UITextField *withField = [[UITextField alloc] initWithFrame:CGRectMake(59, with.frame.origin.y+40, 202, 30)];
withField.placeholder = @"test3";
withField.borderStyle = UITextBorderStyleRoundedRect;
[_homeView addSubview:whoWasIn];
[_homeView addSubview:with];
[_homeView addSubview:whoWasInField];
[_homeView addSubview:withField];
[self movePlusButton];
}
注意:我也尝试过更改框架,但遇到了同样的问题。它从我放置到现有位置的新位置开始动画。
【问题讨论】:
-
即使我将 movePlusButton 方法放在 addSubViews 之前,它仍然不起作用。这里很困惑
-
MovePlusButton 被调用了吗?
-
如何设置您的
_addMoreFieldsBtn?粘贴显示 alloc/init/addsubview 的代码。 -
我创建了一个空项目,插入了您的代码,添加了一个小加号按钮 (
addMoreFieldsBtn) 和一个空视图 (homeView)。我在模拟器中对其进行了测试,按钮按预期跳到了 (30, 30) 位置。我建议你也创建一个空的测试项目,看看它是否适合你。 -
@ScootaP 我敢打赌,如果您关闭自动布局,这将有效。单击情节提要中的一个视图,然后在“文件检查器”窗格中,取消选中“自动布局”。这样能解决吗?