简体   繁体   English

如何在中心设置多个UIView

[英]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. 调用viewDidLayoutSubviews来通知视图控制器其视图刚刚布局了其子视图。

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. 您应该使用bounds而不是frame ,因为如果视图具有转换,则frame是未定义的。

You can also try this 您也可以尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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