ios - 快速加载 View 后自定义自定义键盘高度

标签 ios swift ios8 custom-keyboard

我正在编写一个自定义键盘,我想自定义高度.. Apple 文档仅在 Objective-C 中提供该代码,有谁知道如何用 Swift 语言编写它?这是苹果的代码:

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];

我试着这样写,但它什么也没做..:

override func viewDidAppear(animated:Bool) {
    super.viewDidAppear(true)

   let nib = UINib(nibName: "KeyboardView", bundle: nil)
    let objects = nib.instantiateWithOwner(self, options: nil)
    view = objects[0] as UIView;

    let _viewHeight: CGFloat = 256

    let const1 = NSLayoutConstraint(
        item:self.view, attribute:.Height,
        relatedBy:.Equal, toItem:nil,
        attribute:.NotAnAttribute,multiplier:0, constant: _viewHeight)

    view.addConstraint(const1)

 } 

请帮帮我!

最佳答案

您有一个multiplier:0,它应该是multiplier:1.0

您还可以将 self.viewview =objects[0] 混合作为 UIView。您应该向主 self.view 添加一个约束,即 = self.inputView 并在其上添加自定义 View 。

let customView = objects[0] as UIView
customView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubView(customView)

//layout
let left = NSLayoutConstraint(item: customView, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1.0, constant: 0.0)
let top = NSLayoutConstraint(item: customView, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: 0.0)
let right = NSLayoutConstraint(item: customView, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: 0.0)
let bottom = NSLayoutConstraint(item: customView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
self.view.addConstraints([left, top, right, bottom])

let const1 = NSLayoutConstraint(
    item:self.view, attribute:.Height,
    relatedBy:.Equal, toItem:nil,
    attribute:.NotAnAttribute,multiplier:1.0, constant: _viewHeight)
self.view.addConstraint(const1)

关于ios - 快速加载 View 后自定义自定义键盘高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27888653/

相关文章:

objective-c - 当我不在 dealloc 中释放它时,为什么 : @property(copy) NSString* name, 没有内存泄漏?

ios - 如何在滑动操作后更新 tableview 行?

ios - 水平方向 UICollectionView 与 UIRefreshControl reloadData() 不工作

iOS swift : Getting repeated value while updating 2D Array in custom UITableView cell

swift - 我该去哪里? swift

ios - 我应该自己清除 fsCachedData 文件夹吗?

objective-c - 核心数据 : Automatically select next table row when record deleted

ios - 如何计算文本字段 iOS 中的字符数

swift - 在 Swift 中结合 IF LET 和 OR

swift - Swift 中的音频转换(MP3 到 PCM)