ios - 以编程方式创建 UIViewController 不显示 subview 或识别手势

标签 ios swift uiviewcontroller

我正在尝试完全以编程方式创建一个 View Controller ,以将其用作可从我的应用程序中的任何场景访问的滑出式菜单。该应用程序中还有许多其他 VC 使用 Storyboard(希望有一天我能够完全删除 Storyboard)。我的代码是这样的:

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

我尝试的几乎所有方法都不起作用。当我展示 View Controller 时,我没有看到上面有两个按钮的堆栈 View ,似乎根本无法识别滑动手势,所以我无法关闭 VC,如果我取消注释约束线,我会得到执行它们时访问错误。

我在应用程序的其他部分使用了约束、堆栈 View 、手势(诚然,它们背后总是有一个 Storyboard),但这是我第一次尝试取消它们,这让我很困惑。我希望能够看到和使用按钮(一旦我能看到它们,我就会为它们添加目标),使用滑动关闭 VC,并使堆栈 View 占据整个 View ,似乎没有一个正在工作。我可能遗漏了一些简单的东西,但我一生都看不到它。

最佳答案

您将 UIStackView 声明为计算属性,它必须是惰性变量

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

这样你就破坏了 View 的布局

关于ios - 以编程方式创建 UIViewController 不显示 subview 或识别手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50535385/

相关文章:

ios - xcodebuild no such module 'SwiftyJSON' while build in xcode works

ios - 如何在 iOS 中获取安全区域的顶部、底部、右侧、左侧值?

swift - 异步调用后更新 UITableView 的 numberOfRowsInSection

ios - 从他的 super 调用 View Controller 内的函数

ios - 汉堡菜单/侧菜单外观错误

iphone - iOS阵列初始化错误

ios - Objective-C 协议(protocol)委托(delegate)

swift - 使用 swift 强制刷新另一个 ViewController 组件

ios - 使用 UIWindow() 进行单元测试时出现 NSInternalConsistencyException

ios - UIView突然消失了