ios 如何水平居中 UIView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28614969/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:54:54  来源:igfitidea点击:

How can I horizontally center a UIView?

iosswift

提问by Mohammad Nurdin

How can I make UIViewhorizontally centered and 30px from the top of the view controller using CGRectMake?

如何UIView使用 使视图控制器水平居中且距视图控制器顶部 30 像素CGRectMake

var draggableView: DraggableView = DraggableView(frame:CGRectMake(0 , 30, 260, 260))
draggableView.center = center

回答by Slarty Bartfast

Swift 3;

斯威夫特 3;

subView.center.x = view.center.x

回答by nanothread59

Try:

尝试:

let size = 260
let screenWidth = self.view.frame.size.width

let frame = CGRectMake((screenWidth / 2) - (size / 2), 30, size, size)
let draggableView = DraggableView(frame: frame)

self.view.addSubview(draggableView)

回答by rob mayoff

You cannot center a view in a view controller, because a view controller is not a view and doesn't have a coordinate system.

您不能在视图控制器中居中视图,因为视图控制器不是视图并且没有坐标系。

You can center a view relative to another view. Presumably you want to center draggableViewrelative to the view controller's view. Assuming selfis the view controller, and draggableViewwill be a direct subview of the view controller's view:

您可以将视图相对于另一个视图居中。大概你想draggableView相对于视图控制器的视图居中。假设self是视图控制器,并且draggableView将是视图控制器视图的直接子视图:

var draggableView: DraggableView = DraggableView(frame:CGRectMake(0 , 30, 260, 260))
self.view.addSubview(draggableView)
draggableView.center = self.view.center

回答by ericgu

Although Rob Mayoff is correct, it might be worthwhile to consider centering your frame in viewWillLayoutSubviews, which is called upon device rotation. You can also use the properties center.xand center.yof center to align based on axis. No need to call self in this instance.

尽管 Rob Mayoff 是正确的,但考虑将框架居中放置在viewWillLayoutSubviews 中可能是值得的,它会在设备旋转时调用。您还可以使用center的属性center.xcenter.y根据轴对齐。在这种情况下不需要调用 self 。

override func viewWillLayoutSubviews() {
    draggableView.center = view.center
}

回答by pansora abhay

I am using the 'MMMaterialDesignSpinner' library and creating a custom activity indicator in center of view. It's working correctly, you can change your custom view name like 'DraggableView'. I hope it's usable by you. This code is added into the viewDidLoadfunction in swift 3.

我正在使用“ MMMaterialDesignSpinner”库并在视图中心创建自定义活动指示器。它工作正常,您可以更改自定义视图名称,如“DraggableView”。我希望它对你有用。此代码已添加到viewDidLoadswift 3 中的函数中。

var loader = MMMaterialDesignSpinner()
let size:CGFloat = 40.0
let screenWidth = self.view.frame.size.width
let screenHeight = self.view.frame.size.height

loader = MMMaterialDesignSpinner(frame: CGRect(x: (screenWidth / 2) - (size / 2), y: (screenHeight / 2) - (size / 2), width: size, height: size))

loader.tintColor = UIColor.blue
loader.lineWidth = 4.0
loader.lineCap = kCALineCapRound;
view.addSubview(loader)

回答by dimpiax

Swift 5

斯威夫特 5

// in viewDidLoad or viewWillLayoutSubviews
draggableView.frame.origin.x = view.bounds.midX - draggableView.bounds.midX

回答by Ryan Cocuzzo

This is a neat little extension for centering a view or object:

这是一个用于将视图或对象居中的简洁扩展:

extension CGPoint {
    static var Center: CGPoint {
        return CGPoint(x: UIScreen.main.bounds.maxX/2, y: UIScreen.main.bounds.maxY/2)
    }
}

And then use it like this:

然后像这样使用它:

textField.center = CGPoint.Center

回答by sagar gawande

activityindicater is the view which you want to put in the center

activityindicer 是您要放在中心的视图

self.activityindicater.center = self.baseView.center

self.activityindicer.center = self.baseView.center