【发布时间】:2014-08-28 19:39:45
【问题描述】:
我正在为 iOS8 制作自定义键盘,并且在 Apple 开发人员文档中它说您可以在初始主视图绘制在屏幕上后随时更改自定义键盘的高度。它说要做到这一点,你应该使用 .addConstaint() 方法。
我正在使用 Swift。键盘的初始高度为 215 像素。我有一个向上滑动的手势,将高度增加到 350 像素。哪个按预期工作。向下滑动可将高度更改为 300 像素。
一切正常,但问题是它只能工作一次。我向上滑动,高度增加,向下滑动,高度减小,但如果我再次向上滑动,则没有任何反应。如果我再次向下滑动,则没有任何反应。
因此,如果有人可以查看我的两个函数并告诉我我做错了什么,我将非常感激。
代码如下:
// IBActions
@IBAction func action1(sender: AnyObject) {
if topboxvisible == false {
topboxvisible = true
UIView.animateWithDuration(0.08, delay: 0, options: .CurveEaseIn, animations: {
self.topbox.frame.offset(dx: 0, dy: 40)
}, completion: nil)
}
let expandedHeight:CGFloat = 300
let heightConstraint = NSLayoutConstraint(item:self.view,
attribute: .Height,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: 0.0,
constant: expandedHeight)
self.view.removeConstraint(heightConstraint)
self.view.addConstraint(heightConstraint)
}
@IBAction func action2(sender: AnyObject) {
if topboxvisible == true {
topboxvisible = false
UIView.animateWithDuration(0.08, delay: 0, options: .CurveEaseOut, animations: {
self.topbox.frame.offset(dx: 0, dy: -40)
}, completion: nil)
}
let expandedHeight:CGFloat = 350
let heightConstraint = NSLayoutConstraint(item:self.view,
attribute: .Height,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: 0.0,
constant: expandedHeight)
self.view.removeConstraint(heightConstraint)
self.view.addConstraint(heightConstraint)
}
【问题讨论】:
标签: ios xcode swift autolayout ios8