我正在尝试完全以编程方式创建一个View Controller,以便将其用作从我的应用中的任何场景访问的滑出菜单。应用程序中有许多其他VC使用故事板(有一天我将能够完全删除故事板)。我的代码是这样的:
import UIKit
class MenuViewController: UIViewController {
var stkMenu: UIStackView {
let s = UIStackView(frame: view.frame)
s.axis = .vertical
s.distribution = .fillEqually
s.alignment = .fill
s.translatesAutoresizingMaskIntoConstraints = false
return s
}
var butSettings: UIButton = {
let b = UIButton()
b.backgroundColor = UIColor.clear
b.setTitle("SETTINGS", for: .normal)
b.showsTouchWhenHighlighted = true
b.titleLabel?.font = UIFont(name: "Roboto-Bold", size: 20)
b.setTitleColor(UIColor.netScoreGreen(), for: .normal)
b.translatesAutoresizingMaskIntoConstraints = false
return b
}()
var butAbout: UIButton = {
let b = UIButton()
b.backgroundColor = UIColor.clear
b.setTitle("ABOUT", for: .normal)
b.showsTouchWhenHighlighted = true
b.titleLabel?.font = UIFont(name: "Roboto-Bold", size: 20)
b.setTitleColor(UIColor.netScoreGreen(), for: .normal)
b.translatesAutoresizingMaskIntoConstraints = false
return b
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.black
stkMenu.addArrangedSubview(butSettings)
stkMenu.addArrangedSubview(butAbout)
view.addSubview(stkMenu)
view.translatesAutoresizingMaskIntoConstraints = false
let swipe = UISwipeGestureRecognizer(target: self.view, action: #selector(dismissVC))
self.view.addGestureRecognizer(swipe)
swipe.direction = .right
swipe.isEnabled = true
// NSLayoutConstraint.activate([
// stkMenu.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
// stkMenu.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50),
// stkMenu.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
// stkMenu.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20)
// ])
view.layoutIfNeeded()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc func dismissVC() {
self.dismiss(animated: true, completion: nil)
}
}
我用这个尝试的几乎所有东西都不起作用。当我呈现视图控制器时,我看不到带有两个按钮的堆栈视图,滑动手势似乎根本没有被识别,所以我无法解除VC,如果我取消注释约束线,我得到在执行它们时会失败。
我在应用程序的其他部分使用了约束,堆栈视图和手势(不可否认,它们背后总是有一个故事板),但这是我第一次尝试取消它们并让我感到难过。我希望能够看到并使用按钮(我会在看到它们后为它们添加目标),使用滑动关闭VC,并使堆栈视图占据整个视图,其中任何一个看起来都不是工作。我可能错过了一些简单但却无法看到它的生活。
答案 0 :(得分:2)
您将UIStackView
声明为计算属性,它必须是惰性var
lazy var stkMenu: UIStackView = {
let s = UIStackView(frame: CGRect.zero)
s.axis = .vertical
s.distribution = .fillEqually
s.alignment = .fill
s.translatesAutoresizingMaskIntoConstraints = false
return s
}()
也不要忘记取消评论约束并评论此行
view.translatesAutoresizingMaskIntoConstraints = false
因为你破坏了视图的布局
答案 1 :(得分:1)
看起来你只是错误地输入了声明,而且你不需要设置堆栈视图的框架......
您的代码:
var stkMenu: UIStackView {
let s = UIStackView(frame: view.frame)
s.axis = .vertical
s.distribution = .fillEqually
s.alignment = .fill
s.translatesAutoresizingMaskIntoConstraints = false
return s
}
有效的代码(通过取消注释约束):
var stkMenu: UIStackView = {
let s = UIStackView() //frame: view.frame)
s.axis = .vertical
s.distribution = .fillEqually
s.alignment = .fill
s.translatesAutoresizingMaskIntoConstraints = false
return s
}()