【问题标题】:Centering two side-by-side objects将两个并排的对象居中
【发布时间】:2015-12-15 09:52:37
【问题描述】:

我正在尝试遵循此处描述的解决方案:

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

但这对我不起作用。

这些是属性检查器中我最终得到的约束的屏幕截图:

谁能看出我哪里出错了?

谢谢!

【问题讨论】:

  • 为了让这个问题更容易回答,如果您将问题编辑为具体说明 a)您想要什么、b)您尝试过的内容以及 c)您当前的结果看起来会很有帮助像。只是链接到一个不同的问题并说“我想要那个,但它不起作用”会让你的问题对每个人都更难。

标签: ios xcode autolayout


【解决方案1】:

您扭转了约束 - 图像视图的前导/尾随必须连接到超级视图的中心才能实现您想要做的事情。

【讨论】:

  • 你能给我一步一步的指导吗?
  • 你的第一个项目是 Superview.Trailing,它应该是 PickerView.Trailing(或Leading),你的第二个项目是 PickerView.Trailing,它应该是 Superview.CenterX。
  • 我如何得到它以便第一项是 PickerView.Trailing?我控制点击左侧的pickerview,然后拖动到superview的右上角。我应该做一些不同的事情吗?
  • Control-drag 从第一个视图(pickerview)到第二个(superview)应该可以工作 - 如果没有,请尝试相反的方式。然后选择约束,点击第二项下拉框,选择Center X。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    以下是您需要达到的目标的屏幕截图。我将图像视图的内容模式保持在宽高比匹配。如果您需要更清晰的信息,请告诉我。

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



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

    【讨论】:

      【解决方案4】:

      我现在意识到问题所在了。我已经在 Storyboard 上添加了所有正确的约束,但我没有向 View Controller 添加必要的代码。我只是在创建项目时使用 Xcode 自动生成的视图控制器(我什至没有为两个选择器视图创建 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.
      }
      

      }

      感谢您的所有回复!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-10
        • 2023-01-29
        • 1970-01-01
        • 1970-01-01
        • 2015-06-26
        • 1970-01-01
        • 2013-07-09
        相关资源
        最近更新 更多