【问题标题】:UIButton does not move if called after addSubView如果在 addSubView 之后调用 UIButton 不会移动
【发布时间】: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 我敢打赌,如果您关闭自动布局,这将有效。单击情节提要中的一个视图,然后在“文件检查器”窗格中,取消选中“自动布局”。这样能解决吗?

标签: iphone ios uibutton


【解决方案1】:

问题是 iOS 6 / Xcode 4.5 中的新项目默认启用“自动布局”。 Autolayout 是“Springs and Struts”的替代品(但它仅适用于 iOS 6)。此功能将约束添加到优先于您在代码中尝试的移动的视图。

所以有三种可能的解决方法:

1) 以编程方式在按钮上创建新约束。 Autolayout 非常强大和灵活……特别是如果您尝试同时支持 iPhone 5 和更早型号的占用空间。您可以通过观看 WWDC 视频找到有关如何执行此操作的更多信息:Introduction to Auto Layout for iOS and OS X

2) 不要使用自动布局。在 Storyboard 中选择一个视图,然后在 File Inspector 中取消选中“Use Autolayout”。

3) 为按钮上的每个约束创建 IBOutlets。然后在移动按钮之前,移除这些约束:

@interface MyViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *addMoreFieldsBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *vConstraint;
- (IBAction)addMoreFields:(id)sender;
@end

还有……

-(void)movePlusButton {
    NSLog(@"Moving button");
    [self.view removeConstraint:self.hConstraint];
    [self.view removeConstraint:self.vConstraint];
    [UIButton beginAnimations:nil context:nil];
    [UIButton setAnimationDuration:0.3];
    _addMoreFieldsBtn.center = CGPointMake(30,30);
    [UIButton commitAnimations];
}

(你需要调用removeConstraints:的实际视图是按钮的父视图,可能是也可能不是self.view)。

【讨论】:

    【解决方案2】:

    实际上,我认为发生的事情是您的子视图首先进入屏幕,因此具有优先权,然后一旦子视图被删除,如果您更改代码,按钮就会移动:

    [_homeView addSubview:whoWasIn];
    [_homeView addSubview:with];
    [_homeView addSubview:whoWasInField];
    [_homeView addSubview:withField];
    
    [self movePlusButton];
    

    }//移动[self movePlusButton];最多 6 行代码,还是写在第一行

    [self movePlusButton];
    [_homeView addSubview:whoWasIn];
    [_homeView addSubview:with];
    [_homeView addSubview:whoWasInField];
    [_homeView addSubview:withField];
    

    这应该可以解决你所有的问题

    :)

    【讨论】:

    • 您是否在启用自动布局的情况下测试了这段代码?我试过了,它对我不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多