我有一个UIViewController
,其上有四个UIButtons
(2 x 2),我在接口构建器中完美地设置了它。我将获得免费且广告支持的应用版本,因此我需要根据应用是付费还是广告支持的版本重新加载该场景。基于此,我试图使用视觉格式语言来布局视图。我的UIButton
高度的值不正确,尽管我在计算它们时考虑了它们。我无法弄清楚我的错误(或遗漏?)。
我对界面构建器中的按钮没有约束,但我确实已将IBOutlets
连接到MyViewController
。 MyViewController
位于导航控制器中,底部有一个标签栏。
我创建了一个名为layoutButtons
的方法,我在viewDidLoad
之后调用了super.viewDidLoad()
。这是:
func layoutButtons() {
// Configure layout constraints
// Remove interface builder constraints from storyboard
view.removeConstraints(view.constraints)
// create array to dump constraints into
var allConstraints = [NSLayoutConstraint]()
// determine screen size
let screenSize: CGRect = UIScreen.mainScreen().bounds
let navBarRect = navigationController!.navigationBar.frame
let navBarHeight = navBarRect.height
let tabBarRect = tabBarController!.tabBar.frame
let tabBarHeight: CGFloat = tabBarRect.height
// calculate button width based on screen size
// padding for left + middle + right = 8.0 + 8.0 + 8.0 = 24.0
let buttonWidth = (screenSize.width - 24.0) / 2
/*
My buttons are extending under the top & bottom layout guides despite accounting
for them when I set the buttonHeight.
*/
// padding for top + middle + bottom = 8.0 + 8.0 + 8.0 = 24.0
let buttonHeight = (screenSize.height - topLayoutGuide.length - bottomLayoutGuide.length - 24.0) / 2
// create dictionary of metrics
let metrics = ["buttonWidth": buttonWidth,
"buttonHeight": buttonHeight,
"navBarHeight": navBarHeight,
"tabBarHeight": tabBarHeight,
"bannerAdWidth": bannerAdWidth,
"bannerAdHeight": bannerAdHeight]
// create dictionary of views
var views: [String : AnyObject] = ["firstButton": firstButton,
"secondButton": secondButton,
"thirdButton": thirdButton,
"fourthButton": fourthButton,
"topLayoutGuide": topLayoutGuide,
"bottomLayoutGuide": bottomLayoutGuide]
let topRowHorizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-[firstButton(buttonWidth)]-[secondButton(buttonWidth)]-|",
options: [.AlignAllCenterY],
metrics: metrics,
views: views)
allConstraints += topRowHorizontalConstraints
let bottomRowHorizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-[thirdButton(buttonWidth)]-[fourthButton(buttonWidth)]-|",
options: [.AlignAllCenterY],
metrics: metrics,
views: views)
allConstraints += bottomRowHorizontalConstraints
let leftColumnVerticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|[topLayoutGuide]-[firstButton(buttonHeight)]-[thirdButton(buttonHeight)]-[bottomLayoutGuide]|",
options: [],
metrics: metrics,
views: views)
allConstraints += leftColumnVerticalConstraints
let rightColumnVerticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|[topLayoutGuide]-[secondButton(buttonHeight)]-[fourthButton(buttonHeight)]-[bottomLayoutGuide]|",
options: [],
metrics: metrics,
views: views)
allConstraints += rightColumnVerticalConstraints
NSLayoutConstraint.activateConstraints(allConstraints)
}
我已经摆弄了我的buttonHeight
变量,但我尝试过的每次迭代都会导致topLayoutGuide
和bottomLayoutGuide
下的按钮生成。这是它在运行时的样子:
我欢迎任何建议在哪里寻找我的错误。谢谢你的阅读。
答案 0 :(得分:0)
我最初关注的是Ray Wenderlich's Visual Format Language tutorial,教程的设置与我的类似,因为subViews
位于故事板上,并通过MyViewController
连接到IBOutlets
。
出于绝望,我修改了故事板并在代码中创建了UIButtons
。一路上,我发现我的问题是我在按钮布局之前设置了图像。 故事的寓意是UIButtons
上的图像不会被设置好!
下面是我viewDidLoad
中一个方法调用的代码,该方法列出了一个2 x 2的按钮网格:
// Configure Buttons
firstButton.translatesAutoresizingMaskIntoConstraints = false
// code to customize button
secondButton.translatesAutoresizingMaskIntoConstraints = false
// code to customize button
thirdButton.translatesAutoresizingMaskIntoConstraints = false
// code to customize button
fourthButton.translatesAutoresizingMaskIntoConstraints = false
// code to customize button
// Add buttons to the subview
view.addSubview(firstButton)
view.addSubview(secondButton)
view.addSubview(thirdButton)
view.addSubview(fourthButton)
// create views dictionary
var allConstraints = [NSLayoutConstraint]()
let views: [String : AnyObject] = ["firstButton": firstButton,
"secondButton": secondButton,
"thirdButton": thirdButton,
"fourthButton": fourthButton,
"topLayoutGuide": topLayoutGuide,
"bottomLayoutGuide": bottomLayoutGuide]
// Calculate width and height of buttons
// 24.0 = left padding + middle padding + right padding
let buttonWidth = (screenSize.width - 24.0) / 2
let buttonHeight = (screenSize.height - topLayoutGuide.length - bottomLayoutGuide.length - 24.0) / 2
// Create a dictionary of metrics
let metrics = ["buttonWidth": buttonWidth,
"buttonHeight" : buttonHeight]
let topVerticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|[topLayoutGuide]-[firstButton(buttonHeight)]",
options: [],
metrics: metrics,
views: views)
allConstraints += topVerticalConstraints
let topRowHorizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-[firstButton(buttonWidth)]-[secondButton(==firstButton)]-|",
options: NSLayoutFormatOptions.AlignAllCenterY,
metrics: metrics,
views: views)
allConstraints += topRowHorizontalConstraints
let bottomRowHorizontalContraints = NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-[thirdButton(buttonWidth)]-[fourthButton(==thirdButton)]-|",
options: NSLayoutFormatOptions.AlignAllCenterY,
metrics: metrics,
views: views)
allConstraints += bottomRowHorizontalContraints
let leftColumnVerticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(
"V:[firstButton]-[thirdButton(==firstButton)]-[bottomLayoutGuide]-|",
options: [],
metrics: metrics,
views: views)
allConstraints += leftColumnVerticalConstraints
let rightColumnVerticalConstriants = NSLayoutConstraint.constraintsWithVisualFormat(
"V:[secondButton(buttonHeight)]-[fourthButton(==secondButton)]-[bottomLayoutGuide]-|",
options: [],
metrics: metrics,
views: views)
allConstraints += rightColumnVerticalConstriants
NSLayoutConstraint.activateConstraints(allConstraints)
// ** DON'T SET IMAGES ON THE BUTTONS UNTIL THE BUTTONS ARE LAID OUT!!!**
firstButton.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
firstButton.setImage(UIImage(named: "first.png"), forState: .Normal)
secondButton.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
secondButton.setImage(UIImage(named: "second.png"), forState: .Normal)
thirdButton.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
thirdButton.setImage(UIImage(named: "third.png"), forState: .Normal)
fourthButton.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
fourthButton.setImage(UIImage(named: "fourth.png"), forState: .Normal)