我不知道我是否错过了什么,但当我评论这一行时
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”作为侧面菜单显示在另一个视图控制器上的代码。我希望在我的超级视图的一半上呈现侧边菜单。
答案 0 :(得分:0)
您需要修复第二个约束。从:
let bottomConstraint = NSLayoutConstraint(item: sideMenuView.view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
为:
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
至少这是我认为你想要的。除非您绝对需要支持iOS 8,否则您可以考虑切换布局锚点。将约束代码缩短为:
class MyModel(models.Model):
thing = models.ForeignKey('Thing')
更容易关注恕我直言。