Xcode - 宽度和水平位置不明确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31173666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode - Width and Horizontal Position Are Ambiguous
提问by Infamous911
I'm trying to figure out how autolayout works and I have run into a problem where I don't understand why I am getting a warning that "The width and horizontal position are ambiguous" for a view. The view is the bottom thin blue line as you can see in the screenshot of the ViewController I am dealing with:
我试图弄清楚自动布局是如何工作的,但我遇到了一个问题,我不明白为什么我会收到“视图的宽度和水平位置不明确”的警告。视图是底部的蓝色细线,如您在我正在处理的 ViewController 的屏幕截图中所见:
The arrow and home buttons do not have any constraints. The constraints for the larger gray view is as follows:
箭头和主页按钮没有任何限制。较大灰度视图的约束如下:
The constraints for the "Authors" label is as follows:
“作者”标签的限制如下:
And the constraints for the blue thin view is as follows:
蓝色细视图的约束如下:
I don't understand why I would be getting this warning since the larger gray view isn't getting the warning but there doesn't seem to be any difference in how I treat the two views. I made sure to include constraints on leading and trailing space for both of them so why is the horizontal position ambiguous for the thin blue one? I can kind of see why the width might be ambiguous but I don't understand why it wouldn't be ambiguous for the larger gray view. Shouldn't autolayout automatically deal with it? I want the width obvious to scale with the screen size.
我不明白为什么我会收到这个警告,因为较大的灰色视图没有收到警告,但我对待这两个视图的方式似乎没有任何区别。我确保包括对它们两个的前导和尾随空间的限制,那么为什么薄蓝色的水平位置不明确?我可以理解为什么宽度可能不明确,但我不明白为什么对于较大的灰色视图它不会不明确。autolayout 不应该自动处理吗?我希望宽度明显随屏幕尺寸缩放。
采纳答案by Bishow Gurung
Its better not to put such view inside the top bar, just below the greybar would be best for it if your are doing it just for partition. But I have resolved for your way Here.
最好不要将这样的视图放在顶部栏内,如果您只是为了分区而在灰色栏下方,则最适合它。但我已经解决了你的方式在这里。
For blue bar:
对于蓝条:
- You are giving "bottom space to:", its not necessary as it is maintained by "height equals:2"
- you are giving "leading space to" and "Align to x" thats too don't make sense.
- 您正在给“底部空间:”,它没有必要,因为它由“高度等于:2”维护
- 你给“前导空间”和“对齐到x”这太没有意义了。
Solution:
解决方案:
Just do this->
就这样做->
- Give "leading space to: superview(greyview)" and "Trailing space to: superview(greyview)" both value 0
- Give "top space to:(authors)" certain value
- And at last "Height Equals to: 2" or as you wish.
- 将“前导空间:superview(greyview)”和“尾随空间:superview(greyview)”都设为0
- 给“顶部空间:(作者)”一定的价值
- 最后“高度等于:2”或如您所愿。
Constraints of greyView
greyView 的约束
Constraints of label
标签的约束
Constraints of Blue bar
蓝条的约束
回答by Mike Taverne
Remove Align Center X constraint from the blue thin view.
从蓝色薄视图中删除对齐中心 X 约束。
Side note: it looks like you are trying to create a standard navigation bar. If so, there is a better way to do this:
旁注:看起来您正在尝试创建标准导航栏。如果是这样,有一个更好的方法来做到这一点:
Select the controller you want as the root of your navigation flow (your "Home" controller, perhaps). Then go to Editor > Embed In > Navigation Controller. This will put your view controller inside a navigation controller. You can then wire up a Show segue to your Authors controller, and it will automatically have a navigation bar. At runtime the back arrow to Home will appear automatically.
选择您想要的控制器作为导航流的根(可能是您的“主页”控制器)。然后转到编辑器 > 嵌入 > 导航控制器。这会将您的视图控制器放在导航控制器中。然后你可以将 Show segue 连接到你的 Authors 控制器,它会自动有一个导航栏。在运行时,返回主页的箭头将自动出现。
EDIT:
编辑:
Say you have Home and Authors view controllers.
假设您有 Home 和 Authors 视图控制器。
- Embed the Authors view controller in a navigation controller.
- Create a Show segue from a button on the Home view controller to the navigation controller that contains the Authors view controller.
Within your Authors view controller
viewDidLoad
, add a Home button to dismiss the Authors view controller, like this:self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(title: "Home", style: UIBarButtonItemStyle.Bordered, target: self, action: "goHome"), animated: true)
Add a function to dismiss your Authors view controller when the Home button is tapped:
func goHome() { self.navigationController!.dismissViewControllerAnimated(true, completion: nil) }
- 将 Authors 视图控制器嵌入到导航控制器中。
- 创建从 Home 视图控制器上的按钮到包含 Authors 视图控制器的导航控制器的 Show segue。
在您的 Authors 视图控制器中
viewDidLoad
,添加一个 Home 按钮以关闭 Authors 视图控制器,如下所示:self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(title: "Home", style: UIBarButtonItemStyle.Bordered, target: self, action: "goHome"), animated: true)
添加一个函数来在点击 Home 按钮时关闭你的 Authors 视图控制器:
func goHome() { self.navigationController!.dismissViewControllerAnimated(true, completion: nil) }
You would need to do a little extra to get the arrow icon on your Home button. This postgives some ideas how to do this. If you already have the image, it should be very easy.
您需要做一些额外的工作才能在主页按钮上显示箭头图标。这篇文章给出了一些如何做到这一点的想法。如果您已经有了图像,那应该很容易。
Now your Authors view controller is within a real navigation controller, and you can navigate from it to other view controllers, say if user taps an author to get more information.
现在你的 Authors 视图控制器在一个真正的导航控制器中,你可以从它导航到其他视图控制器,比如用户点击一个作者来获取更多信息。