[英]How to set the frame of a ViewController without storyboard
I want to create a view (eg 100x100) with ViewController without using the storyboard. 我想使用ViewController创建一个视图(例如100x100)而不使用情节提要。 I am wondering what the best way to declare the frame of the ViewController is. 我想知道声明ViewController框架的最佳方法是什么。
I tried: 我试过了:
class MyLittleViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
}
}
And I tried to see this view on my MainViewController: 我试图在MainViewController上看到此视图:
override func viewDidLoad() {
super.viewDidLoad()
let myLittleView = MyLittleViewController()
myLittleView.willMove(toParent: self)
myLittleView.view.translatesAutoresizingMaskIntoConstraints = false
myLittleView.view.backgroundColor = .red
view.addSubview(myLittleView.view)
// enable auto-sizing (for example, if the device is rotated)
myLittleView.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addChild(myLittleView)
myLittleView.didMove(toParent: self)
myLittleView.view.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myLittleView.view.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
It doesn't work like I expected because the little view doesn't appear on the main view. 它不像我预期的那样工作,因为小视图没有出现在主视图上。 Any hints? 有什么提示吗?
You shouldn't mix frame layout with auto-layout set a width & height constraints also 您也不应将框架布局与自动布局设置宽度和高度限制相混合
NSLayoutConstraint.activate([
myLittleView.view.centerXAnchor.constraint(equalTo: view.centerXAnchor),
myLittleView.view.centerYAnchor.constraint(equalTo: view.centerYAnchor),
myLittleView.view.heightAnchor.constraint(equalToConstant:100),
myLittleView.view.widthAnchor.constraint(equalToConstant:100)
])
OR 要么
myLittleView.view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
myLittleView.view.center = view.center
OR 要么
override func loadView() {
view = UIView()
view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
}
Edit: 编辑:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .green
let myLittleView = MyLittleViewController()
myLittleView.willMove(toParent: self)
myLittleView.view.backgroundColor = .red
view.addSubview(myLittleView.view)
// enable auto-sizing (for example, if the device is rotated)
myLittleView.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addChild(myLittleView)
myLittleView.didMove(toParent: self)
myLittleView.view.center = view.center
}
}
class MyLittleViewController: UIViewController {
override func loadView() {
view = UIView()
view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
}
}
Another way to set frame of the view controller is override method loadView() and set the view frame like this 设置视图控制器框架的另一种方法是重写方法loadView()并像这样设置视图框架
func loadView() {
view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.