我做了一个自定义的键盘工具栏:

ios - UIToolbar不适合屏幕-LMLPHP

为此,我创建了一个工具栏

let keyboardToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 44))

和横幅的视图
adToolbar = GADBannerView(frame: CGRectMake(0, 44, self.view.bounds.size.width, 44))

然后我将它们分组在另一个UIToolbar中(我也尝试过UIView)
let clusterView = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 88))
clusterView.addSubview(adToolbar)
clusterView.addSubview(keyboardToolbar)

然后将视图添加到UITextField的键盘上。

一切正常,但是当我旋转设备时会发生以下情况:

ios - UIToolbar不适合屏幕-LMLPHP
(clusterView UIToolbar正确调整了大小,但两个包含的栏没有正确调整...)

我尝试过
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    methodWhichGeneratesToolbar()
}

但是,是一样的,我该怎么解决这个问题?

P.S.: I've made an example project.

最佳答案

这是有关如何使用Visual Format Language(VFL)进行编程自动布局的建议。您需要查看the VFL docs才能理解VFL字符串语法(它们将外部视图固定在主视图的顶部和侧面,并将两个子视图固定在内部和彼此之间,并将其高度设置为44 )。

我没有安装AdMob,所以我使用了常规的UIView而不是横幅视图,但希望它的大小应类似-该代码在测试应用程序的9.2仿真器上可以正常运行

let keyboardToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 44))
keyboardToolbar.translatesAutoresizingMaskIntoConstraints = false //This is critical for all programmatic autolayout - if you forget it nothing will work


let adToolbar = UIView(frame: CGRectMake(0, 44, self.view.bounds.size.width, 44))
adToolbar.translatesAutoresizingMaskIntoConstraints = false


let clusterView = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 88))
clusterView.translatesAutoresizingMaskIntoConstraints = false

//Map views to keys used in visual format language strings
let views = ["keyboardToolbar":keyboardToolbar,"adToolbar":adToolbar,"clusterView":clusterView]

//Map values to strings used in vfl strings
let metrics = ["barHeight":44]

//In named variables to make it clear what they are
//Syntax is explained in link above
let verticalConstraintsStr = "V:|[keyboardToolbar(barHeight)][adToolbar(barHeight)]|"
let adHorizontalConstraintsStr = "|[adToolbar]|"
let keyboardHorizontalConstraintsStr = "|[keyboardToolbar]|"

let subViewConstraintStrs = [
  verticalConstraintsStr,
  adHorizontalConstraintsStr,
  keyboardHorizontalConstraintsStr
]


//Views must be added to subviews before adding constraints
// if the superview is referenced using
//the | symbol in the VFL strings

clusterView.addSubview(keyboardToolbar)
clusterView.addSubview(adToolbar)

//Converts strings to constraints for subviews and add them

for constraintStr in subViewConstraintStrs {
  let allConstraints = NSLayoutConstraint.constraintsWithVisualFormat(constraintStr, options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
  clusterView.addConstraints(allConstraints)

}

let clusterVerticalConstraintsStr = "V:|[clusterView]" //Note no | at the end - no bottom pin
let clusterHorizontalConstraintsStr = "|[clusterView]|"

view.addSubview(clusterView)

//Same process for the enclosing view

for constraintStr in [clusterVerticalConstraintsStr,clusterHorizontalConstraintsStr] {
        let allConstraints = NSLayoutConstraint.constraintsWithVisualFormat(constraintStr, options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
        view.addConstraints(allConstraints)
    }

VFL功能强大,但调试起来很烦人,并且不能执行所有类型的约束(例如,不对齐-您必须使用更为冗长的API进行约束)。

10-07 16:08