Centering two side-by-side objects

∥☆過路亽.° 提交于 2020-01-07 03:05:23

问题


I'm trying to follow the solution described here:

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

But it is not working for me.

These are the screenshots from the Attribute Inspector for the constraints that I ended up with:

Can anyone see where I am going wrong?

Thanks!


回答1:


You turned the constraints around - the leading/trailing of the image views have to be connected to the center of the superview to achieve what you want to do.




回答2:


Whatever I do, I always get SuperView.Trailing in the first item. Maybe I have it set up wrong? This is what I have:




回答3:


Here are the screenshots of what you require to achieve. I have kept image views' content mode to aspect fit. Let me know if you need more clarity.

  1. Scene containing views (I've set superview's background color to red):



  2. View hierarchy with constraints:




回答4:


I realise now what the issue was. I had put in all the correct constraints on the Storyboard but I had not added the necessary code to the View Controller. I was simply using the View Controller that is automatically generated by Xcode when you create a project (I had not even created IB outlets for the two picker views).

Now, when I add this code into the VC, the layout with the two side-by-side pickers displays fine:

import UIKit

class 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.
}

}

Thanks for all your responses!



来源:https://stackoverflow.com/questions/34285928/centering-two-side-by-side-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!