简体   繁体   English

Xcode自动布局约束不起作用(使用情节提要)

[英]Xcode Auto Layout Constraints not working (using storyboard)

I have been using Auto Layout programaticaly, but now I want to see what the storyboard feature in Xcode is all about. 我一直在使用Auto Layout编程,但是现在我想看看Xcode中的故事板功能是什么。 I think I have created my constraints for the first button (the button on the bottom left) correctly: 我想我已经为第一个按钮(左下角的按钮)正确创建了约束:

接口构建器

I don't seem to have any conflicting or ambiguous constraints, however when I run the app on my phone to test, it looks like this: 我似乎没有任何冲突或模棱两可的约束,但是当我在手机上运行该应用进行测试时,它看起来像这样:

电话屏幕

As you can see, the button is in the top left...and no matter what constraints I try to use, the button always sticks to the top left. 如您所见,该按钮位于左上方...无论我尝试使用什么约束,该按钮始终位于左上方。 I want it to be in the bottom left hand corner. 我希望它位于左下角。

Any help would be appreciated, thanks. 任何帮助,将不胜感激,谢谢。

UPDATE UPDATE

I've tried the suggestions and neither have worked...here is how I have it setup now - I had to do left:-16 top:0 right:-16 bottom:0 to get the mapView constraints to work. 我尝试了建议,但都没有奏效...这是我现在的设置方法-我必须做左:-16上:0:右:-16下:0才能使mapView约束起作用。 For the button I used the insets option (as posted in an answer) and did bottom:0, left:0 - so as to pin it to the bottom left corner (only two constraints neccessary?): 对于按钮,我使用了insets选项(如答案中所述),并执行了bottom:0,left:0-以便将其固定到左下角(仅需要两个约束吗?):

更新

Clear all contrains and: 清除所有禁忌素并:

1) pin MapView: 0 - 0 - 0 - 0 from Super View 2) pin button: 0(left) - 0 (bottom) from MapView 3) add width and heigth contraint 1)从Super View固定MapView:0-0-0-0 2)从MapView固定Pin按钮:0(左)-0(底部)3)增加宽度和高度约束

It should works 应该可行

In your screenshot, the mapView seems to be some sort of custom class not the MKMapView you can drag from the Interface Builder. 在屏幕快照中,mapView似乎是某种自定义类,而不是您可以从Interface Builder中拖动的MKMapView

I just tried it and if you would have the normal MKMapView , you would not be able to add subviews to the map. 我只是尝试过,如果您拥有普通的MKMapView ,则将无法向地图添加子视图。 This is the setup I tried and all works as it is supposed to. 这是我尝试过的设置,所有操作均按预期进行。

带按钮的地图视图

My suggestion would be to move the buttons not the same level as the mapView (so that they have the same superView ). 我的建议是将按钮移动到与mapView不同的级别(以使它们具有相同的superView )。 Then you add your constraints and all should work just fine. 然后添加约束,所有约束都应该可以正常工作。

I think that the mapView has it's own way of handling subviews so maybe it just clears the constraints you add in the storyboard. 我认为mapView具有处理子视图的独特方式,因此也许它只是清除了您在情节提要中添加的约束。

Let me know how it went! 让我知道一切如何! Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM