[英]Center a view inside viewcontroller in iOS Playground app
I'm currently building a swift playground book. 我目前正在制作一本快速的操场书。 Is it possible to center a view inside a view controller while running on an iPad ?
在iPad上运行时,是否可以将视图居中放置在视图控制器中?
Here's my loadView
function of my view controller: 这是我的视图控制器的
loadView
函数:
override public func loadView() {
let view = UIView()
view.backgroundColor = .white
view.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
self.view = view
let frame = CGRect(x: 0, y: 0, width: 375, height: 375)
let newView = UIView(frame: frame)
view.addSubview(newView)
}
How can I center the newView
in the view of the view controller? 如何在视图控制器的视图中居中
newView
?
Thanks ! 谢谢 !
You can try to do it using Autolayout: 您可以尝试使用自动版式进行操作:
let frame = CGRect(x: 0, y: 0, width: 375, height: 375)
let newView = UIView(frame: frame)
view.addSubview(newView)
newView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
newView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
newView.leftAnchor.constraint(equalTo: view.leftAnchor),
newView.rightAnchor.constraint(equalTo: view.rightAnchor),
newView.widthAnchor.constraint(equalTo: newView.heightAnchor),
])
newView.backgroundColor = UIColor.red
UPDATE 更新
Playground code: 游乐场代码:
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .red
view.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
self.view = view
let frame = CGRect(x: 0, y: 0, width: 375, height: 375)
let newView = UIView(frame: frame)
newView.backgroundColor = .blue
view.addSubview(newView)
newView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
newView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
newView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
// need to define its size too
newView.heightAnchor.constraint(equalToConstant: 375),
newView.widthAnchor.constraint(equalTo: view.heightAnchor),
])
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
In this Xcode playground, notice the view controller's view
bounds don't yet match the simulator size in viewDidLoad
(). 在此Xcode游乐场中,请注意,视图控制器的
view
边界尚未与viewDidLoad
()中的模拟器大小匹配。 The simulator seems to default to iPad size but then display at iPhone size. 该模拟器似乎默认为iPad大小,但随后以iPhone大小显示。 In an Xcode project however, this isn't the case — you can get the current simulator bounds in
viewDidLoad()
. 但是,在Xcode 项目中,情况并非如此-您可以在
viewDidLoad()
获得当前模拟器的边界。
In either case, a better place to position your subviews is in viewDidLayoutSubviews()
. 无论哪种情况,在
viewDidLayoutSubviews()
放置子视图的最佳位置。
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
var yellowView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
yellowView.backgroundColor = .yellow
yellowView.frame = CGRect(x: 0, y: 0, width: 250, height: 250)
view.addSubview(yellowView)
}
override func viewDidLayoutSubviews() {
yellowView.center = CGPoint(x: view.bounds.width / 2, y: view.bounds.height / 2)
}
}
let vc = MyViewController()
PlaygroundPage.current.liveView = vc
像这样:
newView.center = self.view.center
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.