如何使用.center将子视图放置在父视图的中心?(How to place a subview in the center of parent view using .center?)

编程入门 行业动态 更新时间:2024-10-18 10:25:15
如何使用.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) }

}

这里是一个屏幕:

I have two subviews: the purple one belongs to a main view and it works wonderful with the syntax:

sSubview.center = view.center

The orange one belongs to the purple view and the same syntax doesn't place it correctly in the center. Could you see where my mistake is?

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) }

}

Here is a screen:

最满意答案

尝试下面的代码秒子视图:

sLabel.center = CGPoint(x: sSubview.frame.width / 2, y: sSubview.frame.height / 2)

你试图在中间放置一个标签,但是它根据第一个Bubu帧计算,哪个原点不是0.0

Try the next code for seconds subview:

sLabel.center = CGPoint(x: sSubview.frame.width / 2, y: sSubview.frame.height / 2)

You're trying to place a label in the center, but it calculated according to the firstSbuview frame, which origin is not 0.0

更多推荐

本文发布于:2023-07-17 13:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   如何使用   中心   center   view

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!