-2

我在从 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])

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

4

2 回答 2

2

我有同样的问题。我假设您没有在绿色块上设置高度,因为您希望它填满键盘的剩余空间。如果您希望键盘具有恒定的高度,您可以简单地在绿色块上设置一个高度约束,您就完成了,但是由于屏幕尺寸和方向,您可能不希望所有东西都使用一个高度。

在 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()
}

//***************************
于 2016-09-15T21:57:21.823 回答
0

如果我遗漏了有关该问题的任何内容,请告诉我。如果需要更多信息,请使用评论,我会更新答案。

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

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

于 2016-09-02T17:59:57.617 回答