【问题标题】:Custom Keyboard Storyboard with Xcode 8 beta带有 Xcode 8 beta 的自定义键盘故事板
【发布时间】:2017-01-03 07:01:24
【问题描述】:

我在使用 Xcode 8 beta 6 中的情节提要设计自定义键盘时遇到了一些问题。
出于某种原因,当我在 iOS 10 设备上启动键盘时,结果如下:

这就是我在故事板中的设计方式,顶视图:


底视图:



所以它只显示底部视图的高度。
我在 iOS 9 上没有这个问题。
关于出了什么问题的任何想法?

更新
这就是键盘在 iOS 9 中的加载方式:

更新 2:
即使在 viewDidLoad() 中以这种方式以编程方式创建视图也不起作用:

self.view.backgroundColor = UIColor.orange

let bottomView = UIView()

bottomView.backgroundColor = UIColor.blue

self.view.addSubview(bottomView)

bottomView.translatesAutoresizingMaskIntoConstraints = false

let trailing = bottomView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
let leading = bottomView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor)
let bottom = bottomView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -50)
let top = bottomView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0)

NSLayoutConstraint.activate([trailing, leading, bottom, top])

我写信给苹果错误报告。
希望能得到他们的消息

【问题讨论】:

  • 人们对这个问题投了反对票。能否请他们在 cmets 中写下他们这样做的原因?

标签: storyboard swift3 custom-keyboard ios10 xcode8-beta6


【解决方案1】:

我也有同样的问题。我假设您没有在绿色块上设置高度,因为您希望它填满键盘的剩余空间。如果您希望键盘的高度保持不变,您只需在绿色块上设置一个高度约束即可,但由于屏幕尺寸和方向的原因,您可能不希望所有东西都使用一个高度。

在 iOS 9 中,自定义键盘的默认大小与系统键盘相同。因此,如果您在 iOS 9 中运行它,绿色块会根据这些尺寸填充剩余空间。由于某种原因,在 iOS 10 中没有默认高度,并且由于您的绿色块没有高度限制,它认为高度为零。

要修复它,您需要为键盘设置一个高度。这是我为处理它而编写的代码(到目前为止还不错)。将它放在 ViewDidLoad 之前的 keyboardviewcontroller 类中,您应该可以开始了:

//***************************

//create constraint variable before function
var constraint = NSLayoutConstraint()

//function to set height
func setKeyboardHeight () {
    let screenSize = UIScreen.mainScreen().bounds.size
    let screenH = screenSize.height;

    self.view.removeConstraint(constraint)

    //you can set the values below as needed for your keyboard
    if screenH >= 768 {
        //for iPad landscape or portrait
        self.constraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: 300.0)
        self.view.addConstraint(self.constraint)

    } else if screenH >= 414 {
        //for iPhone portrait AND iPhone Plus landscape or portrait
        self.constraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: 220.0)
        self.view.addConstraint(self.constraint)

    } else {
        //for iPhone landscape
        self.constraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: 140.0)
        self.view.addConstraint(self.constraint)
    }
}

//sets height when keyboard loads
override func updateViewConstraints() {
    super.updateViewConstraints()
    // Add custom view sizing constraints here
    setKeyboardHeight()
}

//sets or changes height when device rotates
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    setKeyboardHeight()
}

//***************************

【讨论】:

  • 谢谢。我会尽快尝试这个。这个解释似乎说得对。
【解决方案2】:

如果我在这个问题上遗漏了什么,请告诉我。如果需要更多信息,请使用 cmets,我会更新答案。

如果您希望扩展底部视图,我认为您可能需要降低顶部视图的内容压缩阻力优先级。如果您想保持 topView 和 bottomView 之间的比例间距,您的问题尚不清楚。

您可以在模拟器或设备上运行应用程序,然后打开视图调试并尝试找出 topView 发生了什么。它很可能是零大小或隐藏在一些意想不到的视图后面。在视图调试中,您希望单击视图以查看其轮廓。还有一个选项可以查看约束,以便您可以确定哪个约束可能会以意想不到的方式影响视图。

【讨论】:

  • 我不能调试键盘的视图。我转到“调试视图层次结构”,但收到消息“无法捕获视图层次结构”。我不明白为什么 topView 没有扩展到顶部。如您所见,我设置了约束。
猜你喜欢
  • 2016-04-24
  • 2018-06-10
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多