UIButton does not move if called after addSubView

So, I'm trying to move the UIButton after clicking it.

The _addMoreFields method _addMoreFields called after the button is clicked.

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

The weird part - if I comment on the addSubView code, then the button moves.

If I save this code, the button does not 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 problem. He starts the animation from a new place that I put in an existing place.

+4
source share
2 answers

The problem is that in new projects in iOS 6 / Xcode 4.5, the "Autostart" function is enabled by default. Autolayout is a replacement for "Springs and Struts" (but it only works with iOS 6). This function adds restrictions to the view, which takes precedence over the move that you tried to execute in your code.

So there are three possible fixes:

1) Create new button restrictions programmatically. Autolayout is pretty powerful and flexible ... especially if you're trying to keep track of iPhone 5 and earlier. You can find more information on how to do this by watching the WWDC video: Introduction to auto-layout for iOS and OS X

2) Do not use Autolayout. Select the view in your storyboard, and in the "File inspector" uncheck the "Use self-timer" box.

3) Create IBOutlets for each of the restrictions on the button. Then, before moving the button, remove these restrictions:

 @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 that you need to call removeConstraints: on is the parent view of the button, which may or may not be self.view).

+5
source

In fact, I think that it happens that your slave device first appears on the screen, and therefore takes precedence, and then, as soon as the subview is deleted, the button will move if you change the code:

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

} // move [self movePlusButton]; up to 6 lines of code or make the first line

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

This should solve all your problems.

:)

0
source

Source: https://habr.com/ru/post/1440175/


All Articles