2016-04-09 178 views
0

我需要一些嚴重的幫助與自動佈局。我總共有12個按鈕,我試圖將其作爲鍵盤佈局。它最終應該看起來像這樣。AutoLayout約束或StackView

enter image description here

目前,它看起來像這樣

enter image description here

這裏是我目前的制約因素。

enter image description here

我真的需要一些幫助工作這個out.Can有人任何人的幫助。我是編程和XCODE的新手。但是這不應該這麼難。我可以請一些幫助。我在教程後觀看了關於自動佈局的教程,這是一個非常混亂的概念。我需要佈置這個簡單的鋼琴鍵盤,請幫助。

感謝

回答

0

我會建議以編程方式嘗試和設置您的約束,而不能在接口生成器。通過這種方式,您將能夠了解實際發生的約束條件,並且能夠在界面構建器中輕鬆完成。

有三種類型的編程Autolay約束表示法: 1. init(item:attribute:relatedBy:toItem:attribute:multiplier:constant :)。當您需要基於百分比的佈局(乘數)時最好使用。 2.錨符號(iOS9中的新增功能)。我沒有使用新的錨符號的經驗,但我知道它與界面構建器中約束的組成相似。但是,你再也不能確切地知道約束對你的每個觀點做了什麼。 3. constraintsWithVisualFormat。與第一個相同,但更簡單。唯一不利的一面是,如果你打算按比例估算你的觀點,那麼它就沒有這樣的能力。

我想讓你測試3號只是因爲你可以更多的「可視化」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和基金會在文件的頂部。

然後在viewDidLoad方法下方添加以下代碼到你的ViewController:

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

您可以更改框架,以符合您的需求。

我們正在做的是將一個自定義UIView添加到接口。你可以在沒有自定義UIView的情況下在你的ViewController上完成所有約束設置,只需在你的ViewController類中添加KeyboardView類的屬性和「setup」方法即可。

對於複雜的視圖設置喜歡你的我會建議以編程方式進行設置,因爲你總是要改變一切,但不是讓所有的東西都與界面構建器分離,你將能夠精確地移動你想要的值。

通過更改度量值,短劃線和括號之間的值來進行實驗。然後嘗試用符號1重新創建約束條件。

也知道您可以隨時自定義和子類化UIKit的元素。

+0

安德烈斯,這工作!現在它沒有按照我想要的方式工作,因爲它完全消除了我的所有圖像。我想我需要微調這個來獲得我想要的,但這是我收到的第一個答案,讓我走上正軌。非常感謝。 – blaq