UIToolbar不适合屏幕

时间:2016-01-17 20:48:08

标签: ios swift rotation autolayout uitoolbar

我制作了一个自定义键盘工具栏:

enter image description here

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

$(this).attr('src');

和横幅视图

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

然后我将它们分组到另一个UIToolbar(我也试过UIView)

adToolbar = GADBannerView(frame: CGRectMake(0, 44, self.view.bounds.size.width, 44))

我将视图添加到UITextField键盘。

一切都好,但是当我旋转设备时会发生这种情况:

clusterView UIToolbar resize correctly but not the two contained bars... (clusterView UIToolbar正确调整大小,但不包含两个包含的栏...)

我试过

let clusterView = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 88))
clusterView.addSubview(adToolbar)
clusterView.addSubview(keyboardToolbar)

但它是一样的,我可以尝试解决这个问题吗?

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

1 个答案:

答案 0 :(得分:2)

这是一个关于如何使用可视化格式语言(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)。