如何用Swift改变iOS 8自定义键盘高度



我正在使用Swift为iOS 8制作自定义键盘。我是iOS开发新手,无法独自解决这个问题。在在线应用程序扩展编程文档中,苹果表示,你可以在启动后更改任何自定义键盘的高度。他们在Objective-C中提供了一个例子,但我使用的是Swift,到目前为止,我似乎找不到Swift版本的代码。

有人能把3行代码转换成Swift或告诉我一种改变键盘高度的方法吗?

下面是带有代码的网页:https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/Keyboard.html

下面是代码行:

CGFloat _expandedHeight = 500;
NSLayoutConstraint *_heightConstraint = 
[NSLayoutConstraint constraintWithItem: self.view 
    attribute: NSLayoutAttributeHeight 
    relatedBy: NSLayoutRelationEqual 
    toItem: nil 
    attribute: NSLayoutAttributeNotAnAttribute 
    multiplier: 0.0 
    constant: _expandedHeight];
[self.view addConstraint: _heightConstraint];

我将提供的代码添加到viewDidAppear方法中,键盘自行调整大小。

下面是有效的代码:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    let expandedHeight:CGFloat = 500
    let heightConstraint = NSLayoutConstraint(item:self.view,
        attribute: .Height,
        relatedBy: .Equal,
        toItem: nil,
        attribute: .NotAnAttribute,
        multiplier: 0.0,
        constant: expandedHeight)
    self.view.addConstraint(heightConstraint)
}
let heightConstraint = NSLayoutConstraint(item: view, 
                                     attribute: .Height, 
                                     relatedBy: .Equal, 
                                        toItem: nil, 
                                     attribute: .NotAnAttribute, 
                                    multiplier: 0.0, 
                                      constant: expandedHeight)

complete,

let expandedHeight:CGFloat = 500
        let heightConstraint = NSLayoutConstraint(item:self.view,
            attribute: .Height,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 0.0,
            constant: expandedHeight)
            self.view.addConstraint(heightConstraint)

最新更新