简体   繁体   English

如何在没有情节提要的情况下设置ViewController的框架

[英]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.

相关问题 故事板:如何使用故事板为 UIPageViewController 设置初始视图控制器? - Storyboard: how to set initial viewcontroller for UIPageViewController with storyboard? 如何在情节提要中加载没有按钮的ViewController? - How to load viewcontroller without button in storyboard? 如何使用情节提要设置条件初始ViewController - How to set conditional initial viewcontroller with storyboard 如何在故事板中设置初始ViewController的委托 - How to set the delegate of initial ViewController in a storyboard 情节提要ViewController的自定义类设置为不带有Xib的ViewController时显示黑色 - Storyboard viewcontroller's custom class set as a viewcontroller without xib shows black 如何在通用应用程序的ViewController中设置UIImageView框架? - How to set UIImageView frame in ViewController for Universal App? 如何在情节提要上设置 UIButtom 的框架或边界 - How to get the frame or bounds of the UIButtom was set on the storyboard 没有StoryBoard的情况下如何在两个ViewController之间使用委托? - How to use delegate between two ViewController without StoryBoard? Swift 4.2:如何在 ViewController 中嵌入 UIPageControl(没有 Storyboard)? - Swift 4.2: How to embed UIPageControl in ViewController (without Storyboard)? 如何在没有情节提要ID的情况下迅速为导航控制器设置根视图控制器。 并导航到其他视图,仅使用viewcontroller名称 - How to set root view controller for a navigation controller in swift without storyboard id. And navigate to other view Only with viewcontroller name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM