xcode UIView 出现错误的中心位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28226408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIView appears with wrong center position
提问by Jonathan H.
In the following function I define all the necessary characteristics of a UIView:
在以下函数中,我定义了 UIView 的所有必要特征:
func OpenController() {
var gameOverView: UIView = UIView()
gameOverView.center = super.view.center
gameOverView.frame.size = CGSize(width: 200, height: 300)
gameOverView.backgroundColor = UIColor.grayColor()
self.view.addSubview(gameOverView)
}
Even though I define the center of the UIView "gameOverView" as that of the the viewcontroller it resides in, it appears with a corner in the center of the viewcontroller and not centered in the viewcontroller. I have tried various other ways of defining the position (NSLayoutConstraints, frame.x, frame.y etc.) but all have this result.
即使我将 UIView“gameOverView”的中心定义为它所在的视图控制器的中心,它在视图控制器的中心出现一个角,而不是在视图控制器的中心。我尝试了各种其他定义位置的方法(NSLayoutConstraints、frame.x、frame.y 等),但都有这样的结果。
If anyone can tell me why this happens and how to center the UIView within its parent view controller I would greatly appreciate it!
如果有人能告诉我为什么会发生这种情况以及如何将 UIView 置于其父视图控制器中,我将不胜感激!
回答by Firo
Your issue here is that your center is being set before the frame. Since you are creating the view without the frame argument your frame is {0, 0}.
您的问题是您的中心设置在框架之前。由于您正在创建没有 frame 参数的视图,因此您的 frame 是 {0, 0}。
So you are currently centering the subview then resizing it, so this is happening:
所以你目前正在居中子视图然后调整它的大小,所以发生了这种情况:
What you need to do is resize the subview then center it, like this:
您需要做的是调整子视图的大小然后将其居中,如下所示:
So you can just swap your centering and framing logic:
所以你可以交换你的居中和框架逻辑:
gameOverView.frame.size = CGSize(width: 200, height: 300)
gameOverView.center = super.view.center
Otherwise even easier just pass the frame when creating the view (you could even pass in the proper x, y coordinates to center it here too):
否则更容易在创建视图时传递框架(你甚至可以传递适当的 x, y 坐标以将其居中):
var gameOverView: UIView = UIView(frame: CGRectMake(0, 0, 200, 300))
回答by LifeIsHealthy
You have to set the center of the view after setting its size. This is because before setting the size or frame of a view its frame rect is (x:0,y:0,width:0,height:0). If you then immediately set its center, then a view of size zero will get centered so its new frame rect could be (x:20,y:20,width:0,height:0) (if the parent view is 40x40). If you now change the view's size, the origin point of the view will not actually move so the new frame could be (x:20,y:20,with:5,height:5) which means the view is no longer centered.
设置视图大小后,您必须设置视图的中心。这是因为在设置视图的大小或框架之前,它的框架矩形是 (x:0,y:0,width:0,height:0)。如果您立即设置其中心,那么大小为零的视图将居中,因此其新框架矩形可能是 (x:20,y:20,width:0,height:0)(如果父视图是 40x40)。如果您现在更改视图的大小,则视图的原点实际上不会移动,因此新框架可能是 (x:20,y:20,with:5,height:5),这意味着视图不再居中。
So to center a view you have to first set its size and the its center.
因此,要使视图居中,您必须首先设置其大小和中心。