Reputation: 75
i have three subview like subview1,subview2,subview3 in a UIViewController
inside a UIView
.
I'm using constraints on them, the constraints which i'm using to show then in a center when the button is clicked are these,
Issue :
When I run the app and click the button
once it comes in the right position. But when i dismiss it and try again to open it, the subview
change its position itself and comes on the top left of the UIViewcontroller
,
As you can see in a screen shot, but i want to show the view
every time when i click the button in the center of the UIViewcontroller
.
I'm confused that constraints are right but why it always changes its position when we open it again.
My code is ,
- (IBAction)Precord:(id)sender {
_DateView.hidden=NO;
[self.DateView bounceIntoView:self.view direction:DCAnimationDirectionLeft];
}
The contents in story board are like this , enter image description here
Upvotes: 0
Views: 758
Reputation: 468
It depends on the way you are dismissing the View. I just tried below code and it worked for me
- (IBAction)btn_Tapped:(id)sender {
_smallView.hidden = false;
[_smallView bounceIntoView:self.view direction:DCAnimationDirectionLeft];
}
- (IBAction)hide_Tapped:(id)sender {
_smallView.hidden = true;
}
Basically i am just hiding the view. And when btn_Tapped, everytime the view is displaying with bouncing animation in the center. Let me know how you go!
Upvotes: 1
Reputation: 2604
Simple way to programmatically add constrains. Modify it with your desire size.
Notice 1: toItem: self, where self is just a view where u added the subView.
Notice 2: SubView MUST be added to self or a subView of self.
subView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: subView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: subView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: subView, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0.5, constant: 0).isActive = true
NSLayoutConstraint(item: subView, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0.5, constant: 0).isActive = true
I would recommend adding constrains in Storyboard. Easier to see/read, clear code and error validator.
On your problem i would say it a animation problem. Just before adding your view run again the method that set the view frames/constrains (to reset the animation position modification).
Or just when animation is finish, instead of hiding the view, remove it from super view. And when u need it again, just add it again.
Upvotes: 0
Reputation: 1874
try this
yourSubView.center = CGPointMake(yourMainView.frame.size.width / 2,
yourMainView.frame.size.height / 2);
Upvotes: 0