[英]Swift: Different title placement depending on configuration
我有两种配置给我不同的结果:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
let rootVC = MainController()
window.rootViewController = rootVC
self.window = window
window.makeKeyAndVisible()
}
class MainController: UIViewController {
var titleLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
titleLabel.text = "Grocery List"
titleLabel.textColor = .black
titleLabel.font = .systemFont(ofSize: 20, weight: .bold)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(titleLabel)
[enter image description here][1]
setupConstraints()
}
func setupConstraints() {
let padding: CGFloat = 15
NSLayoutConstraint.activate([
titleLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: padding),
titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
}
}
但是,如果我这样做
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
window?.rootViewController = UINavigationController(rootViewController: MainController())
}
这不一样。 为什么? 我认为主要问题是我真的不知道配置细节在做什么,所以我很困惑为什么不同的设置会给我不同的结果。 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.