从 XIB 文件加载时,Swift 中的 iOS 键盘太宽太高

标签 ios iphone xcode swift xib

我正在使用 Swift(带有 iOS 8.3 SDK 的 XCode 6)构建键盘,当我加载 xib 时,键盘的宽度和高度大约超出了 1000 像素。我已在自由格式 xib 文件中将所有键放入 UIView 中,并将 UIViews 约束设置为 super View 顶部、底部、左侧和右侧。

如您所见,结果很烦人。这是我的代码:

import UIKit

class KeyboardViewController: UIInputViewController {

var BlurBoardView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    loadInterface()
}

func loadInterface() {
    //load the nib file
    var blurboardNib = UINib(nibName: "BlurBoardView", bundle: nil)
    // initiate the view
    BlurBoardView = blurboardNib.instantiateWithOwner(self, options: nil)[0] as! UIView
    // add the interface to the main view
    view.addSubview(BlurBoardView)
    // copy the background color
    view.backgroundColor = BlurBoardView.backgroundColor

    let blur = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Light))
    blur.frame = view.frame
    view.addSubview(blur)
}

@IBAction func didTapButton(sender: AnyObject?) {

    let button = sender as! UIButton
    let title = button.titleForState(.Normal)
    var proxy = textDocumentProxy as! UITextDocumentProxy

    switch title as String!{
        case "<" :
            proxy.deleteBackward()
        case "RETURN" :
            proxy.insertText("\n")
        case " " :
            proxy.insertText(" ")
        case "CHG" :
            self.advanceToNextInputMode()
        default :
            proxy.insertText(title!)
    }
}

/*
override func textWillChange(textInput: UITextInput) {
    // The app is about to change the document's contents. Perform any preparation here.
}

override func textDidChange(textInput: UITextInput) {
    // The app has just changed the document's contents, the document context has been updated.

    var textColor: UIColor
    var proxy = self.textDocumentProxy as! UITextDocumentProxy
    if proxy.keyboardAppearance == UIKeyboardAppearance.Dark {
        textColor = UIColor.whiteColor()
    } else {
        textColor = UIColor.blackColor()
    }
    //self.nextKeyboardButton.setTitleColor(textColor, forState: .Normal)
}
*/
}

预编译的 xib 中的键盘如下所示:

您还可以查看父(键) View 约束。

我主要是一名 Objective-C 开发人员,所以代码是有道理的,但由于 Swift,问题可能只是一个巨大的 SBE,所以很抱歉,如果它非常简单;)

编辑:P 键有一个约束,将其绑定(bind)到左侧 super View 14 像素,右侧 super View 0 像素(O 键),这就是我知道它太宽的方式。键盘底部约束是 super View 的底部。

最佳答案

看起来您正在使用自动布局,并且没有在 View 和 BlurBoardView 之间设置约束。

当调用 ViewDidLoad 时, View 没有正确的框架。仅当调用 viewDidLayoutSubviews 时才有效。

因此在 ViewDidLoad 中设置 View 和 BlurBoardView 之间的约束。或者将 BlurBoardView.frame = 你想要的框架放在 viewDidLayoutSubview 中。

你的blurView也是如此

关于从 XIB 文件加载时,Swift 中的 iOS 键盘太宽太高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31103167/

相关文章:

iphone - 如何在iPhone中绘制色轮?

iphone - 从另一个类调用方法时为空变量

iphone - 比较 Objective-c 中的 NSDates

xcode - Xcode场景扩展坞已隐藏

xcode - 如何将 "Run Script"构建阶段限制为我的发布配置?

ios - 在 Swift 中将 AVAudioPlayer 输出更改为扬声器?

iphone - 如何判断我们何时与 GameCenter GKMatch session 断开连接?

ios - 使用 ReactiveCocoa 从 View 模型更新 UITableViewCell

iphone - UITableView 中的异步图像加载器

ios - 将信息从 TableView 传递到 View Controller 不是基于 indexPathForSelectedRow,而是基于所选单元格的标签文本?