简体   繁体   English

当我将translationsAutoresizingMaskIntoConstraints转换为false时,视图不显示

[英]view doesn't shows up when I turn translatesAutoresizingMaskIntoConstraints to false

I don't know if I'm missing out something, but when I comment this line 我不知道是否遗漏了一些东西,但是当我评论此行时

sideMenuView.view.translatesAutoresizingMaskIntoConstraints = false

then my side menu shows up but on the whole screen. 然后我的侧边菜单就会出现,但会显示在整个屏幕上 I am setting its constraints programatically as: 我通过编程将其约束设置为:

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

This is the code presenting a view controller "SideMenuViewController" as a side menu over another view controller. 这是将视图控制器“ SideMenuViewController”呈现为另一个视图控制器上方的侧菜单的代码。 I want side menu to be presented but on half of my superview. 我希望显示副菜单,但占一半。

You need to fix your second constraint. 您需要解决第二个约束。 From: 从:

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)

At least that is what I think you intend. 至少那是我认为您打算的。 You might consider switching layout anchors unless you absolutely need to support iOS 8. Would shorten your constraints code to: 除非绝对需要支持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

And is easier to follow IMHO. 并且更容易遵循恕我直言。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我添加约束时,使translatesAutoresizingMaskIntoConstraints为false - Make translatesAutoresizingMaskIntoConstraints false when I added constraints 将autoresizingMaskIntoConstraints转换为不起作用 - translatesAutoresizingMaskIntoConstraints doesn't work 当使用translationsAutoresizingMaskIntoConstraints = false时,UITableview停止响应 - UITableview stops responding when using translatesAutoresizingMaskIntoConstraints=false 将transformsAutoresizingMaskIntoConstraints设置为false时,AutoLayout的行为会异常 - AutoLayout acts strangely when translatesAutoresizingMaskIntoConstraints set to false 对于 ViewController 的“主”视图,您是否必须将 translatesAutoresizingMaskIntoConstraints 设置为 false? - Do you have to set translatesAutoresizingMaskIntoConstraints to false for a ViewController's 'main' view? iOS Swift 约束不起作用,UITableView 的子视图在 translatesAutoresizingMaskIntoConstraints = false 时消失 - iOS Swift Constraints not working, subview of UITableView disappears when translatesAutoresizingMaskIntoConstraints = false 如果 translatesAutoresizingMaskIntoConstraints = false,则滚动视图不起作用 - scrollview not working if translatesAutoresizingMaskIntoConstraints = false 无法设置translatesAutoresizingMaskIntoConstraints - Can't set translatesAutoresizingMaskIntoConstraints 向下滚动视图并返回时,切换开关未保存状态 - Toggle Switch doesn't save is state when i scroll down the view and return up 当我将“translatesAutoresizingMaskIntoConstraints”设置为true,ChangeSizesInRunTime时,为什么Constrains会中断? - Why Constrains Break when I just set “translatesAutoresizingMaskIntoConstraints” to true, ChangeSizesInRunTime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM