AutoLayout约束或StackView

时间:2016-04-09 14:26:36

标签: ipad autolayout swift2 ios9 xcode7

我需要一些严肃的帮助来进行自动布局。我总共有12个按钮,我试图将其布局为键盘。它最终应该是这样的。

enter image description here

目前它看起来像这样

enter image description here

这是我目前的限制。

enter image description here

我真的需要一些帮助来解决这个问题。可以有人帮忙。我是编程和XCODE的新手。但这不应该是这么难。我可以请一些帮助。我在关于自动布局的教程之后看过教程,这是一个非常令人困惑的概念。我需要布置这个简单的钢琴键盘,请帮忙。

由于

1 个答案:

答案 0 :(得分:0)

我建议以编程方式尝试设置约束,而不是在界面构建器中。通过这种方式,您将能够了解约束的实际情况,并且能够在界面构建器中轻松完成这些操作。

有三种类型的程序化Autolayout约束表示法: 1. init(item:attribute:relatedBy:toItem:attribute:multiplier:constant :)。最适合在需要基于百分比的布局(乘数)时使用。 2.锚表示法(iOS9中的新功能)。我没有使用新的Anchor表示法的经验,但我知道它类似于界面构建器中约束的组成。但同样,您无法确切知道约束对每个视图的作用。 3. constraintsWithVisualFormat。与第一名相同,但更简单。唯一不利的一面是,如果你打算按百分比调整你的观点,那么就无法做到这一点。

我希望你只测试3号,因为你可以" Visualize"更多constraintsWithVisualFormat表示法。但在开始使用Visual Format language引用之前,请自行指导。

首先,添加一个新的swift文件。将其命名为KeyBoardView并将下面的代码复制到其中。

class KeyboardView: UIView {
var redKey : UIView!
var orangeKey : UIView!
var yellowKey : UIView!
var greenKey : UIView!
var lightBlueKey : UIView!
var darkBlueKey : UIView!
var purpleKey : UIView!
var blackKey1 : UIView!
var blackKey2 : UIView!
var blackKey3 : UIView!
var blackKey4 : UIView!
var blackKey5 : UIView!


override init(frame: CGRect) {
    super.init(frame: frame)
    self.backgroundColor = UIColor.clearColor()
    setup()
}

required init(coder aDecoder: NSCoder) {
    fatalError("This class does not support NSCoding")
}

func setup () {
    redKey = UIView()
    redKey.backgroundColor = UIColor.redColor()
    redKey.translatesAutoresizingMaskIntoConstraints = false
    self.insertSubview(redKey!, atIndex: 10)

    orangeKey = UIView()
    orangeKey.backgroundColor = UIColor.orangeColor()
    orangeKey.translatesAutoresizingMaskIntoConstraints = false
    self.insertSubview(orangeKey!, atIndex: 10)

    yellowKey = UIView()
    yellowKey.backgroundColor = UIColor.yellowColor()
    yellowKey.translatesAutoresizingMaskIntoConstraints = false
    self.insertSubview(yellowKey, atIndex: 10)

    greenKey = UIView()
    greenKey.backgroundColor = UIColor.greenColor()
    greenKey.translatesAutoresizingMaskIntoConstraints = false
    self.insertSubview(greenKey, atIndex: 10)

    lightBlueKey = UIView()
    lightBlueKey.backgroundColor = UIColor.blueColor()
    lightBlueKey.alpha = 0.5
    lightBlueKey.translatesAutoresizingMaskIntoConstraints = false
    self.insertSubview(lightBlueKey, atIndex: 10)

    darkBlueKey = UIView()
    darkBlueKey.backgroundColor = UIColor.blueColor()
    darkBlueKey.translatesAutoresizingMaskIntoConstraints = false
    self.insertSubview(darkBlueKey, atIndex: 10)

    purpleKey = UIView()
    purpleKey.backgroundColor = UIColor.purpleColor()
    purpleKey.translatesAutoresizingMaskIntoConstraints = false
    self.insertSubview(purpleKey, atIndex: 10)

    blackKey1 = UIView()
    blackKey1.backgroundColor = UIColor.blackColor()
    blackKey1.translatesAutoresizingMaskIntoConstraints = false
    self.insertSubview(blackKey1, atIndex: 11)

    blackKey2 = UIView()
    blackKey2.backgroundColor = UIColor.blackColor()
    blackKey2.translatesAutoresizingMaskIntoConstraints = false
    self.insertSubview(blackKey2, atIndex: 11)

    blackKey3 = UIView()
    blackKey3.backgroundColor = UIColor.blackColor()
    blackKey3.translatesAutoresizingMaskIntoConstraints = false
    self.insertSubview(blackKey3, atIndex: 11)

    blackKey4 = UIView()
    blackKey4.backgroundColor = UIColor.blackColor()
    blackKey4.translatesAutoresizingMaskIntoConstraints = false
    self.insertSubview(blackKey4, atIndex: 11)

    blackKey5 = UIView()
    blackKey5.backgroundColor = UIColor.blackColor()
    blackKey5.translatesAutoresizingMaskIntoConstraints = false
    self.insertSubview(blackKey5, atIndex: 11)

    let views = [
        "redKey" : redKey,
        "orangeKey" : orangeKey,
        "yellowKey" : yellowKey,
        "greenKey" : greenKey,
        "lightBlueKey" : lightBlueKey,
        "darkBlueKey" : darkBlueKey,
        "purpleKey" : purpleKey,
        "blackKey1" : blackKey1,
        "blackKey2" : blackKey2,
        "blackKey3" : blackKey3,
        "blackKey4" : blackKey4,
        "blackKey5" : blackKey5,
    ]

    let colorKeySpan = self.bounds.width/7
    let blackKeyWidth = self.bounds.width/12
    let blackKeyHeight = self.bounds.height/2.5
    let scaleGap = self.bounds.width/6

    let metrics = [
        "colorKeySpan" : colorKeySpan,
        "blackKeyWidth" : blackKeyWidth,
        "blackKeyHeight" : blackKeyHeight,
        "scaleGap" : scaleGap,
    ]

    let redKeyVConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[redKey]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    let redKeyHConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[redKey(colorKeySpan)]-0-[orangeKey]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    self.addConstraints(redKeyVConstraint)
    self.addConstraints(redKeyHConstraint)

    let orangeKeyVConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[orangeKey]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    let orangeKeyHConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[orangeKey(colorKeySpan)]-0-[yellowKey]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    self.addConstraints(orangeKeyVConstraint)
    self.addConstraints(orangeKeyHConstraint)

    let yellowKeyVConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[yellowKey]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    let yellowKeyHConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[yellowKey(colorKeySpan)]-0-[greenKey]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    self.addConstraints(yellowKeyVConstraint)
    self.addConstraints(yellowKeyHConstraint)

    let greenKeyVConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[greenKey]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    let greenKeyHConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[greenKey(colorKeySpan)]-0-[lightBlueKey]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    self.addConstraints(greenKeyVConstraint)
    self.addConstraints(greenKeyHConstraint)

    let lightBlueKeyVConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[lightBlueKey]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    let lightBlueKeyHConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[lightBlueKey(colorKeySpan)]-0-[darkBlueKey]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    self.addConstraints(lightBlueKeyVConstraint)
    self.addConstraints(lightBlueKeyHConstraint)

    let darkBlueKeyVConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[darkBlueKey]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    let darkBlueKeyHConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[darkBlueKey(colorKeySpan)]-0-[purpleKey]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    self.addConstraints(darkBlueKeyVConstraint)
    self.addConstraints(darkBlueKeyHConstraint)

    let purpleKeyVConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[purpleKey]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    let purpleKeyHConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[purpleKey(colorKeySpan)]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    self.addConstraints(purpleKeyVConstraint)
    self.addConstraints(purpleKeyHConstraint)

    let blackKey1VConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[blackKey1(blackKeyHeight)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    let blackKey1HConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|-blackKeyWidth-[blackKey1(blackKeyWidth)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    self.addConstraints(blackKey1VConstraint)
    self.addConstraints(blackKey1HConstraint)

    let blackKey2VConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[blackKey2(blackKeyHeight)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    let blackKey2HConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[blackKey1]-blackKeyWidth-[blackKey2(blackKeyWidth)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    self.addConstraints(blackKey2VConstraint)
    self.addConstraints(blackKey2HConstraint)

    let blackKey3VConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[blackKey3(blackKeyHeight)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    let blackKey3HConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[blackKey2]-scaleGap-[blackKey3(blackKeyWidth)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    self.addConstraints(blackKey3VConstraint)
    self.addConstraints(blackKey3HConstraint)

    let blackKey4VConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[blackKey4(blackKeyHeight)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    let blackKey4HConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[blackKey3]-blackKeyWidth-[blackKey4(blackKeyWidth)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    self.addConstraints(blackKey4VConstraint)
    self.addConstraints(blackKey4HConstraint)

    let blackKey5VConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[blackKey5(blackKeyHeight)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    let blackKey5HConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[blackKey4]-blackKeyWidth-[blackKey5(blackKeyWidth)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: views)
    self.addConstraints(blackKey5VConstraint)
    self.addConstraints(blackKey5HConstraint)
}

}

请记住在文件顶部导入UIKit和Foundation。

然后在viewDidLoad方法中将以下代码添加到ViewController:

let keyboardView = KeyboardView(frame: UIScreen.mainScreen().bounds)
self.view.addSubview(keyView

您可以更改框架以满足您的需求。

我们所做的只是在界面中添加自定义UIView。你可以在没有自定义UIView的情况下在ViewController上设置所有约束,只需添加属性和" setup" KeyboardView类的方法到ViewController类。

对于像您这样的复杂视图设置,我建议您以编程方式进行设置,因为您总是会改变所有内容,但不会让所有内容破坏界面构建器,您将能够准确地移动值你想要的。

通过更改指标的值,破折号和括号之间的值进行实验。然后尝试用符号1重新创建约束。

还要知道,您始终可以自定义和子类化UIKit的元素。