【问题标题】:How to change iOS 8 custom keyboard height with Swift?如何使用 Swift 更改 iOS 8 自定义键盘高度?
【发布时间】:2014-10-18 12:09:55
【问题描述】:

我正在使用 Swift 为 iOS 8 制作自定义键盘。我对 iOS 开发非常陌生,我自己无法解决这个问题。在 App Extension Programming 在线文档中,Apple 表示您可以在任何自定义键盘启动后更改其高度。他们在 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];

【问题讨论】:

标签: ios objective-c iphone swift keyboard


【解决方案1】:

我添加了提供给 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)
}

【讨论】:

  • 但我得到了这个:无法同时满足约束。以下列表中的至少一个约束可能是您不想要的。 translatesAutoresizingMaskIntoConstraints) ( "<0x7ff523267da0 v:><0x7ff5230dd2b0 v:>
【解决方案2】:

完全,

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)

【讨论】:

  • 这只是重复了先前的答案,格式错误。该问题仅询问第 3 行。
  • @Mundi 请阅读@Francis 问的问题Can someone convert the 3 lines of code into Swift 不是第 3 行,请告诉我错误
  • 好的,感谢您指出这一点。仍然没有必要重申显而易见的事实。
【解决方案3】:
let heightConstraint = NSLayoutConstraint(item: view, 
                                     attribute: .Height, 
                                     relatedBy: .Equal, 
                                        toItem: nil, 
                                     attribute: .NotAnAttribute, 
                                    multiplier: 0.0, 
                                      constant: expandedHeight)

【讨论】:

  • 我把你给我的代码放到了我键盘的 .swift 文件中,高度没有变化?你有什么想法吗?
  • 那将是一个不同的问题,对吧?它在 Objective-C 中有效吗?
  • 我只尝试过 Swift。但我把它放在 viewDidLoad 方法中,我刚刚读到我把它放在 viewDidAppear 中,我会尝试一下,让你知道结果。
猜你喜欢
  • 2015-04-18
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
  • 2018-05-24
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多