当我将自动调整大小掩码转换为 false 时视图不显示



我不知道我是否错过了什么,但是当我评论此行

sideMenuView.view.translatesAutoresizingMaskIntoConstraints = false

然后我的侧菜单显示在整个屏幕上。我以编程方式设置其约束为:

let sideMenuView = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("sideMenuID") as! SideMenuViewController
var sideMenuLeftConstraint: NSLayoutConstraint?
var isShowingSideMenu = true
var blackMaskView = UIView(frame: CGRectZero)
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    // Add SideMenuViewController
    addChildViewController(sideMenuView)
    sideMenuView.delegate = self
    sideMenuView.didMoveToParentViewController(self)
    sideMenuView.view.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(sideMenuView.view)
    let topConstraint = NSLayoutConstraint(item: sideMenuView.view, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
    let bottomConstraint = NSLayoutConstraint(item: sideMenuView.view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
    let widthConstraint = NSLayoutConstraint(item: sideMenuView.view, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200)
    sideMenuLeftConstraint = NSLayoutConstraint(item: sideMenuView.view, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: -widthConstraint.constant)
    view.addConstraints([topConstraint, bottomConstraint, sideMenuLeftConstraint!, widthConstraint])
    toggleSideMenu()
}
func toggleSideMenu() {
    isShowingSideMenu = !isShowingSideMenu
    if(isShowingSideMenu) {
        // Hide Side Menu
        sideMenuLeftConstraint?.constant = -sideMenuView.view.bounds.size.width
        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                          self.view.layoutIfNeeded()
                        },
            completion: {(completed) -> Void in
                          self.sideMenuView.view.hidden = true
                        })
        UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut,
            animations: { () -> Void in
                          self.view.layoutIfNeeded()
                          self.blackMaskView.alpha = 0.0
                        },
            completion: { (completed) -> Void in
                          self.blackMaskView.removeFromSuperview()
                        })
    } else {
        // Show Side Menu
        blackMaskView = UIView(frame: CGRectZero)
        blackMaskView.alpha = 0.0
        blackMaskView.translatesAutoresizingMaskIntoConstraints = false
        blackMaskView.backgroundColor = UIColor.blackColor()
        view.insertSubview(blackMaskView, belowSubview: sideMenuView.view)
        let topContraint = NSLayoutConstraint(item: blackMaskView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
        let bottomContraint = NSLayoutConstraint(item: blackMaskView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
        let leftContraint = NSLayoutConstraint(item: blackMaskView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
        let rightContraint = NSLayoutConstraint(item: blackMaskView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0)
        view.addConstraints([topContraint, bottomContraint, leftContraint, rightContraint])
        view.layoutIfNeeded()
        UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut,
            animations: { () -> Void in
                          self.view.layoutIfNeeded()
                          self.blackMaskView.alpha = 0.5
                        },
            completion: { (completed) -> Void in
                          let tapGesture = UITapGestureRecognizer(target: self, action: #selector(LoginUserMapView.tapGestureRecognized))
                          self.blackMaskView.addGestureRecognizer(tapGesture)
                        })
        sideMenuView.view.hidden = false
        sideMenuLeftConstraint?.constant = 0
        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                          self.view.layoutIfNeeded()
                        },
            completion: {(completed) -> Void in
                        })
    }
}
func tapGestureRecognized() {
    toggleSideMenu()
}
@IBAction func sideMenuBtn(sender: AnyObject) {
    toggleSideMenu()
}

这是呈现视图控制器" SidemenuviewController"的代码,作为另一个视图控制器的侧面菜单。我希望提供侧面菜单,但在我的超级浏览中的一半。

您需要修复第二个约束。来自:

let bottomConstraint = NSLayoutConstraint(item: sideMenuView.view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)

to:

let bottomConstraint = NSLayoutConstraint(item: sideMenuView.view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)

至少这就是我认为您打算的。除非您绝对需要支持iOS 8,否则您可以考虑切换布局锚。将您的约束代码缩短为:

        sideMenuView.view.topAnchor.constraintEqualToAnchor(self.view.topAnchor).active = true
        sideMenuView.view.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor).active = true
        let widthConstraint = sideMenuView.view.widthAnchor.constraintEqualToConstant(200)
        widthConstraint.active = true
        self.sideMenuLeftConstraint = sideMenuView.view.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor, constant: -widthConstraint.constant)
        self.sideMenuLeftConstraint?.active = true

,并且更容易跟随IMHO。

最新更新