Swift 安全区域布局未正确激活
Swift Safe Area Layout not activating properly
我正在以编程方式学习一些自动布局,我想在屏幕底部添加一个按钮,就在安全区域上方。
我知道代码是有效的,因为我在另一个项目中测试过它,我认为这是一个冲突,因为我从另一个项目得到这个viewController。
我的按钮代码
private let previousButton : UIButton = {
let button = UIButton(type: .system)
button.setTitle("Prev", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
设置约束的代码
fileprivate func setupBottomControls() {
view.addSubview(previousButton)
previousButton.backgroundColor = .red
view.addSubview(previousButton)
NSLayoutConstraint.activate([
previousButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
previousButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
previousButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
previousButton.heightAnchor.constraint(equalToConstant: 50)
])
}
就像我说的,这段代码在另一个项目中工作,但我认为它有冲突,因为我如何称呼它 viewController。
这是来自第一个视图控制器的代码,它使新的 viewController(GettingStartedController) 被显示,这里将是上面提到的按钮。
func switchVC(){
//enter GettingStarted controller
let controller = GettingStartedController()
view.addSubview(controller.view)
present(UINavigationController(rootViewController: controller), animated: true,completion: nil) //used when no animation is present
}
我认为这里的问题是关于如何更改对 GettingStartedController 的调用以便以正确的方式查看安全区域的任何想法?
检查我的代码解决方案。我在点击按钮时将您的功能添加到按钮,它现在显示 GettingStartedController()
:
private let previousButton : UIButton = {
let button = UIButton(type: .system)
button.setTitle("Prev", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(switchVC), for: .touchUpInside)
return button
}()
fileprivate func setupBottomControls() {
view.addSubview(previousButton)
previousButton.backgroundColor = .red
view.addSubview(previousButton)
NSLayoutConstraint.activate([
previousButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
previousButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
previousButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
previousButton.heightAnchor.constraint(equalToConstant: 50)
])
}
@objc func switchVC(){
//enter GettingStarted controller
let controller = GettingStartedController()
present(UINavigationController(rootViewController: controller), animated: true,completion: nil) //used when no animation is present
}
我正在以编程方式学习一些自动布局,我想在屏幕底部添加一个按钮,就在安全区域上方。
我知道代码是有效的,因为我在另一个项目中测试过它,我认为这是一个冲突,因为我从另一个项目得到这个viewController。
我的按钮代码
private let previousButton : UIButton = {
let button = UIButton(type: .system)
button.setTitle("Prev", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
设置约束的代码
fileprivate func setupBottomControls() {
view.addSubview(previousButton)
previousButton.backgroundColor = .red
view.addSubview(previousButton)
NSLayoutConstraint.activate([
previousButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
previousButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
previousButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
previousButton.heightAnchor.constraint(equalToConstant: 50)
])
}
就像我说的,这段代码在另一个项目中工作,但我认为它有冲突,因为我如何称呼它 viewController。 这是来自第一个视图控制器的代码,它使新的 viewController(GettingStartedController) 被显示,这里将是上面提到的按钮。
func switchVC(){
//enter GettingStarted controller
let controller = GettingStartedController()
view.addSubview(controller.view)
present(UINavigationController(rootViewController: controller), animated: true,completion: nil) //used when no animation is present
}
我认为这里的问题是关于如何更改对 GettingStartedController 的调用以便以正确的方式查看安全区域的任何想法?
检查我的代码解决方案。我在点击按钮时将您的功能添加到按钮,它现在显示 GettingStartedController()
:
private let previousButton : UIButton = {
let button = UIButton(type: .system)
button.setTitle("Prev", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(switchVC), for: .touchUpInside)
return button
}()
fileprivate func setupBottomControls() {
view.addSubview(previousButton)
previousButton.backgroundColor = .red
view.addSubview(previousButton)
NSLayoutConstraint.activate([
previousButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
previousButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
previousButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
previousButton.heightAnchor.constraint(equalToConstant: 50)
])
}
@objc func switchVC(){
//enter GettingStarted controller
let controller = GettingStartedController()
present(UINavigationController(rootViewController: controller), animated: true,completion: nil) //used when no animation is present
}