最后我成功了。我已经从 viewDidLoad 中删除了关于加载 nib 等的代码,并为它创建了一个函数。
这里的功能
func loadInterface() {
// load the nib file
let calculatorNib = UINib(nibName: "KeyboardView", bundle: NSBundle.mainBundle())
// instantiate the view
let calculatorView = calculatorNib.instantiateWithOwner(self, options: nil)[0] as! UIView
self.view.frame = CGRectMake(0, 0, 1024, 352)
let objects = calculatorNib.instantiateWithOwner(self, options: nil)
self.view = objects[0] as! UIView
self.inputView?.addSubview(calculatorView)
}
并在 viewDidLoad 中调用此函数。
override func viewDidLoad() {
super.viewDidLoad()
loadInterface()
self.view.autoresizingMask = UIViewAutoresizing.FlexibleHeight
self.view.translatesAutoresizingMaskIntoConstraints = false
// let nib = UINib(nibName: "KeyboardView", bundle: NSBundle.mainBundle())
// self.view.frame = CGRectMake(0, 0, 1024, 352)
// let objects = nib.instantiateWithOwner(self, options: nil)
// view = objects[0] as! UIView;
var counter = 0
for view in self.view.subviews {
if let btn = view as? UIButton {
btn.layer.cornerRadius = 5.0
btn.translatesAutoresizingMaskIntoConstraints = false
btn.tag = counter
counter++
}
}
}
它解决了一切。