以编程方式创建 UIView 在 Xcode 11.1 中不起作用
Creating UIViews Programmatically doesn't work in Xcode 11.1
我想在 Xcode 11.1 中使用 snapkit 创建一个 UIView 作为容器视图或以编程方式创建,但似乎有问题,我认为 Apple 在 Xcode 中更改了 UIView 11.x (iOS 13.x) 因为我在Xcode.
之前的版本中很容易做到
在 SceneDelegate.swift 中(Apple 已将 window 变量从 AppDelegate.swift 移至 SceneDelegate.swift)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: windowScene)
self.window?.rootViewController = ViewController()
self.window?.makeKeyAndVisible()
}
}
}
ViewController.swift
class ViewController: UIViewController {
var containerView: UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view.backgroundColor = .green
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .blue
setUpView()
}
func setUpView() {
self.view.backgroundColor = .blue
self.view.addSubview(containerView)
containerView.snp.makeConstraints { (make) in
make.centerX.equalTo(self.view.snp.centerX)
make.centerY.equalTo(self.view.snp.centerY)
}
}
}
结果:
要使用自动布局显示视图,您需要提供所有W H X Y
满足布局引擎的要求。但是您错过了 width
和 height
约束。
试试这个:
func setUpView() {
self.view.backgroundColor = .blue
self.view.addSubview(containerView)
containerView.snp.makeConstraints { (make) in
make.width.height.equalTo(100)
make.center.equalTo(self.view)
}
}
您需要设置容器视图的高度和宽度。
containerView.heightAnchor.constraint(equalToConstant: 100).isActive = true
containerView.widthAnchor.constraint(equalToConstant: 100).isActive = true
我想在 Xcode 11.1 中使用 snapkit 创建一个 UIView 作为容器视图或以编程方式创建,但似乎有问题,我认为 Apple 在 Xcode 中更改了 UIView 11.x (iOS 13.x) 因为我在Xcode.
之前的版本中很容易做到在 SceneDelegate.swift 中(Apple 已将 window 变量从 AppDelegate.swift 移至 SceneDelegate.swift)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: windowScene)
self.window?.rootViewController = ViewController()
self.window?.makeKeyAndVisible()
}
}
}
ViewController.swift
class ViewController: UIViewController {
var containerView: UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view.backgroundColor = .green
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .blue
setUpView()
}
func setUpView() {
self.view.backgroundColor = .blue
self.view.addSubview(containerView)
containerView.snp.makeConstraints { (make) in
make.centerX.equalTo(self.view.snp.centerX)
make.centerY.equalTo(self.view.snp.centerY)
}
}
}
结果:
要使用自动布局显示视图,您需要提供所有W H X Y
满足布局引擎的要求。但是您错过了 width
和 height
约束。
试试这个:
func setUpView() {
self.view.backgroundColor = .blue
self.view.addSubview(containerView)
containerView.snp.makeConstraints { (make) in
make.width.height.equalTo(100)
make.center.equalTo(self.view)
}
}
您需要设置容器视图的高度和宽度。
containerView.heightAnchor.constraint(equalToConstant: 100).isActive = true
containerView.widthAnchor.constraint(equalToConstant: 100).isActive = true