简体   繁体   中英

How to set mutiple UIView in centre

I have tried these solutions:
First solution

ansview.center = CGPointMake(viewAnswer.frame.size.width  / 2,
                viewAnswer.frame.size.height / 2);
    ansview.center = CGPointMake(viewAnswer., viewAnswer.height/2)

Second solution

self.ansview.center = CGPointMake(CGRectGetMidX(self.viewAnswer.bounds),
                CGRectGetMidY(self.viewAnswer.bounds));

I have already tried this code. But only one view is set to center. How to set multiple views in center?

viewDidLayoutSubviews is called to notify the view controller that its view has just laid out its subviews.

override func viewDidLayoutSubviews() {
        self.ansview.center = CGPointMake(self.viewAnswer.bounds.size.width / 2, self.viewAnswer.bounds.size.height / 2)
    }

You should use the bounds and not the frame , as the frame is undefined if the view has a transform.

You can also try this

[ansview setCenterX:self.view.centerX];
[ansview setCenterY:self.view.centerY];

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