如何使用 .center 将子视图放置在父视图的中心?
How to place a subview in the center of parent view using .center?
我有两个子视图:紫色的属于一个主视图,它的语法非常好:
sSubview.center = view.center
橙色的属于紫色视图,相同的语法没有将它正确地放在中间。
你能看出我的错误在哪里吗?
Here is my code:
import UIKit
class ViewController: UIViewController {
var sSubview = UIView()
var sLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.backgroundColor = UIColor.lightGray
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews() {
createSimplestSubview()
}
// First subview
func createSimplestSubview() {
sSubview = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width * 0.9, height: view.frame.height * 0.9))
sSubview.backgroundColor = UIColor.purple // just for test, to make it visible
sSubview.center = view.center // that s pretty easy!
view.addSubview(sSubview)
createSimplestLabel()
}
// Second subview
func createSimplestLabel() {
sLabel = UILabel(frame: CGRect(x: 0, y: 0, width: sSubview.frame.width * 0.3, height: sSubview.frame.height * 0.2))
sLabel.backgroundColor = UIColor.orange // just for test, to make it visible
sLabel.center = sSubview.center
sLabel.text = "This label has to be centered!"
sLabel.textAlignment = .center
sLabel.numberOfLines = 0
sSubview.addSubview(sLabel)
}
}
这是一个屏幕:
为秒子视图尝试下一个代码:
sLabel.center = CGPoint(x: sSubview.frame.width / 2, y: sSubview.frame.height / 2)
你试图在中心放置一个标签,但它是根据 firstSbuview 框架计算的,其原点不是 0.0
更好的方法是使用 convertPoint
,因为您的代码的问题是坐标 space 有时不同。
sSubview.center = view.superview.convert(view.center, to: sSubview.superview)
view.center
在view.superview
的坐标space
sSubview.center
在sSubview.superview
的坐标space
我有两个子视图:紫色的属于一个主视图,它的语法非常好:
sSubview.center = view.center
橙色的属于紫色视图,相同的语法没有将它正确地放在中间。 你能看出我的错误在哪里吗?
Here is my code:
import UIKit
class ViewController: UIViewController {
var sSubview = UIView()
var sLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.backgroundColor = UIColor.lightGray
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews() {
createSimplestSubview()
}
// First subview
func createSimplestSubview() {
sSubview = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width * 0.9, height: view.frame.height * 0.9))
sSubview.backgroundColor = UIColor.purple // just for test, to make it visible
sSubview.center = view.center // that s pretty easy!
view.addSubview(sSubview)
createSimplestLabel()
}
// Second subview
func createSimplestLabel() {
sLabel = UILabel(frame: CGRect(x: 0, y: 0, width: sSubview.frame.width * 0.3, height: sSubview.frame.height * 0.2))
sLabel.backgroundColor = UIColor.orange // just for test, to make it visible
sLabel.center = sSubview.center
sLabel.text = "This label has to be centered!"
sLabel.textAlignment = .center
sLabel.numberOfLines = 0
sSubview.addSubview(sLabel)
}
}
这是一个屏幕:
为秒子视图尝试下一个代码:
sLabel.center = CGPoint(x: sSubview.frame.width / 2, y: sSubview.frame.height / 2)
你试图在中心放置一个标签,但它是根据 firstSbuview 框架计算的,其原点不是 0.0
更好的方法是使用 convertPoint
,因为您的代码的问题是坐标 space 有时不同。
sSubview.center = view.superview.convert(view.center, to: sSubview.superview)
view.center
在view.superview
sSubview.center
在sSubview.superview