1

我正在尝试将白色子视图的顶部对齐到视图控制器中容器视图高度的一半。见附图。在旧版本的 Xcode 中,可以按住 CTRL 键从视图拖动到容器视图并选择“与容器视图顶部对齐”,但在新的 Xcode 6.1 中,它们仅提供与“顶部布局指南”的对齐方式。因此,我使用了该约束并设置了约束,如您在图像右侧看到的那样。根据我的阅读,以下等式适用:

属性 1 = 属性 2 * 乘数 + 常数。

所以我想用我的设置来做到这一点: View.top = TopLayoutGuide.bottom * 0.5 + 0

这样白色“视图”的顶部与 TopLayoutGuide.bottom 的一半高度对齐。但是,当我这样做时,结果是 ).5 被完全忽略,并且白色视图的顶部与 TopLayouGuide.bottom 对齐,基本上覆盖了整个屏幕。

有人可以弄清楚我做错了什么,以及将视图顶部对齐到容器高度一半的正确方法是什么?

谢谢

-玛莲娜见下图

4

2 回答 2

3

我在 Xcode 6.1 中遇到了同样的问题。我发现有几种方法可以仅使用情节提要编辑器将视图(几乎)分成两半,方法是添加一个子视图以用作一种基准。iMemon的编程解决方案有效,但它需要额外的代码来约束使用中点作为参考的任何对象,这可能会变得乏味。

要解决您的问题:

  1. 将另一个视图添加到控制器的主视图。
  2. 给它一种在其背景下突出的颜色。
  3. 给它一个高度 1。
  4. 将它移动到主视图的中心,并给它“容器中的垂直中心”约束。
  5. 现在您可以拖动任何对象的顶部或底部以与刚刚添加的视图的框架对齐。您可以通过控制+单击+从任何对象拖动到新视图并从弹出菜单中选择适当的约束来应用约束。
  6. 将基准视图的颜色更改为使其在应用程序中不可见的颜色。我只是使用了一种清晰的颜色,因为我想要对齐的对象也很清晰,而且我有背景。如果 clear 对您不起作用,您可以尝试限制基准视图的宽度以匹配您对齐的对象的宽度,然后给它们相同的颜色。

您可以将这种使用视图作为基准的想法扩展到其他情况,例如与两个对象之间距离的 3/4 点对齐。您可以将对象添加到视图,在其中应用约束,然后移动视图并在其父视图中应用约束。发挥创意并进行试验。

于 2015-02-07T01:50:18.143 回答
0

您无法在 Interface Builder 中实现您想要做的事情。但这可以通过通过代码创建约束来实现。我为您创建了一个示例项目,您可以从这里下载

下面是通过代码添加约束的 ViewController 文件的代码。注意:我在 Interface Builder 中添加了任意约束,因为我们需要获取主视图高度。这些也很重要

//
//  ViewController.swift
//  Test
//
//  Created by FR-MAC on 11/12/14.
//  Copyright (c) 2014 ABC. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var mySubview: UIView!

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

    self.addContraintsForMoviePlayerView(self.mySubview, superView: self.view)

}

func addContraintsForMoviePlayerView(var view:UIView, superView:UIView) -> Void {

    view.setTranslatesAutoresizingMaskIntoConstraints(false)
    let leftConstraint = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: superView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)

    let rightConstraint = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: superView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0)

    let bottomConstraint = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: superView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)

    var topValue:CGFloat = superView.frame.size.height
    topValue = topValue/2

    let topConstraint = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view.superview?, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: topValue)

    // Remove the constraints created in Interface Builder to avoid autolayout constraint issues. We just added those constraints to get the correct height of self.view.frame which we used to create above topConstaint
    self.view.removeConstraints(self.view.constraints())

    //Add the above created constraints
    superView.addConstraints( [leftConstraint, rightConstraint, topConstraint, bottomConstraint] )
}

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

希望这可以帮助你:)

于 2014-11-12T10:55:53.457 回答