简体   繁体   中英

Center the subview in UIView using constraints in IOS

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,

  • Horizantally in Container,
  • Vertically in Container,
  • width and
  • height.

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

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!

try this

yourSubView.center = CGPointMake(yourMainView.frame.size.width  / 2, 
                             yourMainView.frame.size.height / 2);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM