Reputation: 2551
So I am trying to move a UIButton
after it is clicked.
The _addMoreFields
method is called after the button is clicked.
_addMoreFieldBtn
is a global UIButton
. When I click it nothing happens.
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.
Any Ideas?
-(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.
Upvotes: 4
Views: 1768
Reputation: 64
Actually, I think that whats happening is that your subview is coming onto to screen first, and therefore taking precedence, and then once the subview is removed, the button would move, if you change the code:
[_homeView addSubview:whoWasIn];
[_homeView addSubview:with];
[_homeView addSubview:whoWasInField];
[_homeView addSubview:withField];
[self movePlusButton];
}// move the [self movePlusButton]; up 6 lines of code, or make it first line
[self movePlusButton];
[_homeView addSubview:whoWasIn];
[_homeView addSubview:with];
[_homeView addSubview:whoWasInField];
[_homeView addSubview:withField];
This should solve all of your problems
:)
Upvotes: 0
Reputation: 3861
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) 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) Don't use Autolayout. Select a view in your Storyboard, and in the File Inspector, uncheck "Use Autolayout."
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
and...
-(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];
}
(the actual view you need to call removeConstraints:
on is the parent view of the button, which may or may not be self.view).
Upvotes: 5