繁体   English   中英

在iOS Playground应用程序的viewcontroller内部将视图居中

[英]Center a view inside viewcontroller in iOS Playground app

我目前正在制作一本快速的操场书。 在iPad上运行时,是否可以将视图居中放置在视图控制器中?

这是我的视图控制器的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)
}

如何在视图控制器的视图中居中newView

谢谢 !

您可以尝试使用自动版式进行操作:

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

更新

游乐场代码:

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

View Controller生命周期-Xcode Playground和Xcode Project之间的细微差别...

在此Xcode游乐场中,请注意,视图控制器的view边界尚未与viewDidLoad ()中的模拟器大小匹配。 该模拟器似乎默认为iPad大小,但随后以iPhone大小显示。 但是,在Xcode 项目中,情况并非如此-您可以viewDidLoad()获得当前模拟器的边界。

无论哪种情况,在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM