本文介绍了如果在addSubView之后调用,UIButton不会移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图在单击UIButton后移动它.

So I am trying to move a UIButton after it is clicked.

单击按钮后将调用_addMoreFields方法.

The _addMoreFields method is called after the button is clicked.

_addMoreFieldBtn是全局UIButton.当我单击它时,什么也没有发生.

_addMoreFieldBtn is a global UIButton. When I click it nothing happens.

奇怪的是,如果我注释掉addSubView代码,则按钮会移动.

The strange part is if I comment out the addSubView code then the button moves.

如果我保留该代码,则按钮不会移动.

If I keep that code the button doesn't move.

有什么想法吗?

-(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];
}

注意:我也尝试过更改框架,但是遇到了同样的问题.它从我放置到现有位置的新位置开始进行动画制作.

NOTE: I also tried changing the frame but I get the same issue. It starts animating from the new location I put to the existing spot.

推荐答案

问题是iOS 6/Xcode 4.5中的新项目默认情况下启用了自动布局".自动布局是弹簧和支柱"的替代品(但仅适用于iOS 6).此功能向视图添加了约束,该约束优先于您在代码中尝试的移动.

The issue is that new projects in iOS 6 / Xcode 4.5 have "Autolayout" enabled by default. Autolayout is a replacement for "Springs and Struts" (but it only works iOS 6). This feature adds constraints to a view which take precedence over the move you were attempting in your code.

因此,有三个可能的解决方法:

So there are three possible fixes to this:

1)以编程方式在按钮上创建新的约束.自动布局功能强大而灵活……尤其是当您试图同时支持iPhone 5和更早型号的设备时.您可以通过查看WWDC视频找到有关如何执行此操作的更多信息:简介到iOS和OS X的自动版式

1) Create new constraints on the button programmatically. Autolayout is pretty powerful and flexible... especially if you are trying to support the footprint of both the iPhone 5 and earlier models. You can find more info on how to do this by checking out the WWDC video: Introduction to Auto Layout for iOS and OS X

2)不要使用自动版式.在情节提要中选择一个视图,然后在文件检查器中取消选中使用自动布局".

2) Don't use Autolayout. Select a view in your Storyboard, and in the File Inspector, uncheck "Use Autolayout."

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

3) Create IBOutlets for each of the constraints on the button. Then before you move the button, remove those constraints:

@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,也可以不是.)

(the actual view you need to call removeConstraints: on is the parent view of the button, which may or may not be self.view).

这篇关于如果在addSubView之后调用,UIButton不会移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:33