Ipad 自动布局约束或堆栈视图
我需要一些严重的帮助与自动布局。我总共有12个按钮,我正试图把它们布置成键盘。最后应该是这样的 现在看起来是这样的 以下是我目前的限制 我真的需要一些人帮我解决这个问题。有人能帮忙吗。我不熟悉编程和XCODE。但这并不难。我能得到一些帮助吗。我看了一本又一本关于自动布局的教程,这是一个非常令人困惑的概念。我需要布置这个简单的钢琴键盘,请帮忙Ipad 自动布局约束或堆栈视图,ipad,autolayout,swift2,ios9,xcode7,Ipad,Autolayout,Swift2,Ios9,Xcode7,我需要一些严重的帮助与自动布局。我总共有12个按钮,我正试图把它们布置成键盘。最后应该是这样的 现在看起来是这样的 以下是我目前的限制 我真的需要一些人帮我解决这个问题。有人能帮忙吗。我不熟悉编程和XCODE。但这并不难。我能得到一些帮助吗。我看了一本又一本关于自动布局的教程,这是一个非常令人困惑的概念。我需要布置这个简单的钢琴键盘,请帮忙 谢谢我建议您尝试以编程方式设置约束,而不是在interface builder中。通过这种方式,您将能够了解约束的实际情况,并且能够在interfac
谢谢我建议您尝试以编程方式设置约束,而不是在interface builder中。通过这种方式,您将能够了解约束的实际情况,并且能够在interface builder中轻松地执行约束 有三种类型的编程自动布局约束符号: 1.init(item:attribute:relatedBy:toItem:attribute:multiplier:constant:)。最好在需要基于百分比的布局(乘数)时使用。 2.锚定符号(iOS9中新增)。我没有使用新锚定符号的经验,但我知道它与interface builder中约束的组合类似。但同样,您无法确切地知道约束对每个视图的作用。 3.使用可视化格式进行约束。与第一名相同,但更简单。唯一不好的一面是,如果要按百分比调整视图大小,它就没有这样做的能力 我想要你测试数字3只是因为你可以用VisualFormat符号“可视化”更多的约束。但是在你开始打开参考文献来指导你自己之前 首先,添加一个新的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。只需将KeyboardView类上的属性和“setup”方法添加到ViewController类中,就可以在没有自定义UIView的情况下在ViewController上设置所有约束
对于复杂的视图设置,如您的,我建议以编程方式进行设置,因为您总是要更改周围的所有内容,而不是使用interface builder中断所有内容,您将能够准确地移动所需的值
通过更改度量值,即虚线和括号之间的值来进行实验。然后尝试用符号1重新创建中的约束
也要知道,您始终可以自定义UIKit的元素并将其子类化。我建议您尝试以编程方式设置约束,而不是在interface builder中。通过这种方式,您将能够了解约束的实际情况,并且能够在interface builder中轻松地执行约束 有三种类型的编程自动布局约束符号: 1.init(item:attribute:relatedBy:toItem:attribute:multiplier:constant:)。最好在需要基于百分比的布局(乘数)时使用。 2.锚定符号(iOS9中新增)。我没有使用新锚定符号的经验,但我知道它与interface builder中约束的组合类似。但同样,您无法确切地知道约束对每个视图的作用。 3.使用可视化格式进行约束。与第一名相同,但更简单。唯一不好的一面是,如果要按百分比调整视图大小,它就没有这样做的能力 我想要你测试数字3只是因为你可以用VisualFormat符号“可视化”更多的约束。但是在你开始打开参考文献来指导你自己之前 首先,添加一个新的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。只需将KeyboardView类上的属性和“setup”方法添加到ViewController类中,就可以在没有自定义UIView的情况下在ViewController上设置所有约束
对于复杂的视图设置,如您的,我建议以编程方式进行设置,因为您总是要更改周围的所有内容,而不是使用interface builder中断所有内容,您将能够准确地移动所需的值
通过更改度量值,即虚线和括号之间的值来进行实验。然后尝试用符号1重新创建中的约束
还要知道,您始终可以自定义UIKit的元素并将其子类化。Andres,这很有效!现在它没有按我希望的方式工作,因为它完全抹平了我所有的图像。我想我需要对这个进行微调以得到我想要的,但这是我收到的第一个让我走上正轨的答案。非常感谢你,安德烈斯,成功了!现在它没有按我希望的方式工作,因为它完全抹平了我所有的图像。我想我需要对这个进行微调以得到我想要的,但这是我收到的第一个让我走上正轨的答案。非常感谢你。