How to center a subview of UIView

前端 未结 14 1834
無奈伤痛
無奈伤痛 2020-11-30 16:47

I have a UIView inside a UIViewm and I want the inner UIView to be always centered inside the outer one, without it having to resize t

相关标签:
14条回答
  • 2020-11-30 17:12
    func callAlertView() {
    
        UIView.animate(withDuration: 0, animations: {
            let H = self.view.frame.height * 0.4
            let W = self.view.frame.width * 0.9
            let X = self.view.bounds.midX - (W/2)
            let Y = self.view.bounds.midY - (H/2)
            self.alertView.frame = CGRect(x:X, y: Y, width: W, height: H)
            self.alertView.layer.borderWidth = 1
            self.alertView.layer.borderColor = UIColor.red.cgColor
            self.alertView.layer.cornerRadius = 16
            self.alertView.layer.masksToBounds = true
            self.view.addSubview(self.alertView)
    
        })
    
    
    }// calculation works adjust H and W according to your requirement
    
    0 讨论(0)
  • 2020-11-30 17:17

    enter image description here

    Set this autoresizing mask to your inner view.

    0 讨论(0)
  • 2020-11-30 17:19

    Objective-C

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

    Swift

    yourSubView.center = CGPoint(x: yourView.frame.size.width  / 2,
                                 y: yourView.frame.size.height / 2)
    
    0 讨论(0)
  • 2020-11-30 17:19

    1. If you have autolayout enabled:

    • Hint: For centering a view on another view with autolayout you can use same code for any two views sharing at least one parent view.

    First of all disable child views autoresizing

    UIView *view1, *view2;
    [childview setTranslatesAutoresizingMaskIntoConstraints:NO];
    
    1. If you are UIView+Autolayout or Purelayout:

      [view1 autoAlignAxis:ALAxisHorizontal toSameAxisOfView:view2];
      [view1 autoAlignAxis:ALAxisVertical toSameAxisOfView:view2];
      
    2. If you are using only UIKit level autolayout methods:

      [view1 addConstraints:({
          @[ [NSLayoutConstraint
             constraintWithItem:view1
             attribute:NSLayoutAttributeCenterX
             relatedBy:NSLayoutRelationEqual
             toItem:view2
             attribute:NSLayoutAttributeCenterX
             multiplier:1.f constant:0.f],
      
             [NSLayoutConstraint
              constraintWithItem:view1
              attribute:NSLayoutAttributeCenterY
              relatedBy:NSLayoutRelationEqual
              toItem:view2
              attribute:NSLayoutAttributeCenterY
              multiplier:1.f constant:0.f] ];
      })];
      

    2. Without autolayout:

    I prefer:

    UIView *parentView, *childView;
    [childView setFrame:({
        CGRect frame = childView.frame;
    
        frame.origin.x = (parentView.frame.size.width - frame.size.width) / 2.0;
        frame.origin.y = (parentView.frame.size.height - frame.size.height) / 2.0;
    
        CGRectIntegral(frame);
    })];
    
    0 讨论(0)
  • 2020-11-30 17:19

    In c# or Xamarin.ios, we can use like this

    imageView.Center = new CGPoint(tempView.Frame.Size.Width / 2, tempView.Frame.Size.Height / 2);

    0 讨论(0)
  • 2020-11-30 17:24

    With IOS9 you can use the layout anchor API.

    The code would look like this:

    childview.centerXAnchor.constraintEqualToAnchor(parentView.centerXAnchor).active = true
    childview.centerYAnchor.constraintEqualToAnchor(parentView.centerYAnchor).active = true
    

    The advantage of this over CGPointMake or CGRect is that with those methods you are setting the center of the view to a constant but with this technique you are setting a relationship between the two views that will hold forever, no matter how the parentview changes.

    Just be sure before you do this to do:

            self.view.addSubview(parentView)
            self.view.addSubView(chidview)
    

    and to set the translatesAutoresizingMaskIntoConstraints for each view to false.

    This will prevent crashing and AutoLayout from interfering.

    0 讨论(0)
提交回复
热议问题