将两个并排物体对中

时间:2015-12-15 09:52:38

标签: ios xcode autolayout

我试图按照此处描述的解决方案:

XCode 7 Autolayout Constraints: How to make 2 images stay horizontally in the center

但它不适合我。

这些是来自Attribute Inspector的屏幕截图,其中列出了我最终得到的约束:

enter image description here

enter image description here

谁能看到我哪里出错了?

谢谢!

4 个答案:

答案 0 :(得分:0)

你改变了约束 - 图像视图的前导/尾随必须连接到超视图的中心,以实现你想要做的事情。

答案 1 :(得分:0)

无论我做什么,我总是在第一项中获得SuperView.Trailing。也许我说错了?这就是我所拥有的:

enter image description here

答案 2 :(得分:0)

以下是您需要实现的屏幕截图。我已将图像视图的内容模式保持为纵横比。如果您需要更清晰,请告诉我。

  1. 包含视图的场景(我将superview的背景颜色设置为红色):

    Scene containing views

  2. 查看带有约束的层次结构:

    View hierarchy with constraints

答案 3 :(得分:0)

我现在意识到问题是什么。我在Storyboard上输入了所有正确的约束,但我没有将必要的代码添加到View Controller中。我只是在创建项目时使用由Xcode自动生成的View Controller(我甚至没有为两个选择器视图创建IB出口)。

现在,当我将此代码添加到VC中时,带有两个并排选择器的布局显示正常:

import UIKit

类ViewController:UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {

var pickerDataSource = ["inch", "feet","mile","meter","yard","kilometer"];


@IBOutlet weak var picker1: UIPickerView!
@IBOutlet weak var picker2: UIPickerView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    picker1.dataSource = self;
    picker1.delegate = self;

    picker2.dataSource = self;
    picker2.delegate = self;

}


func CreateColor(hexAlpha: Int, hexRed: Int, hexGreen: Int, hexBlue: Int) -> CGColor{
    return UIColor(red: CGFloat(hexRed)/255.0, green: CGFloat(hexGreen)/255.0, blue: CGFloat(hexBlue)/255.0, alpha: CGFloat(hexAlpha)/255.0).CGColor
}


func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerDataSource.count;
}



func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerDataSource[row]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

感谢您的所有回复!

相关问题