简体   繁体   English

在iOS中以编程方式创建UI并自动布局?

[英]Creating a UI programmatically in iOS and autolayout?

I am new to iOS programming and I have been building the UI for an app entirely programmatically. 我是iOS编程的新手,我一直在完全以编程方式为应用程序构建UI。 (I deleted the storyboard file + removed a property from the plist file and have been purely doing it through the code.) I want to understand for certain, in iOS7, is autolayout still occurring automatically? (我删除了故事板文件+从plist文件中删除了一个属性,并纯粹通过代码来完成它。)我想确定,在iOS7中,自动布局仍然会自动发生吗? Or is this not the case? 还是不是这样?

Secondly, how can I manage it entirely programmatically? 其次,我如何完全以编程方式管理它? AKA, I have a nice interface for portrait mode. AKA,我有一个很好的人像模式界面。 However when I go to landscape, it is obvious the UI is not adjusting properly. 但是,当我去风景时,很明显,UI调整不正确。 This makes me think autolayout is not on or it is not using proper constraints. 这使我认为自动布局未启用或未使用适当的约束。

Is it suggested that I actually use interface builder instead? 是否建议我实际上使用接口构建器?

Thanks. 谢谢。

I want to understand for certain, in iOS7, is autolayout still occurring automatically? 我想确定,在iOS7中,自动布局仍然会自动发生吗? Or is this not the case? 还是不是这样?

There are two ways to use auto layout: you can set it inside your storyboard, or you can define it programmatically. 有两种使用自动布局的方法:您可以在情节提要板中设置它,也可以通过编程定义它。 If you do not do either, auto layout is not applied. 如果您也不选择,则不会应用自动布局。

how can I manage it entirely programmatically? 如何完全以编程方式管理它?

As the comments above suggest, have a look a Apple Docs as a start. 就像上面的评论所建议的那样,以Apple Docs为例。 There are also a couple of frameworks on github to make things easier, eg, Masonry , but you will need anyway some understanding of how auto layout works. github上还有两个框架可以使事情变得更容易,例如Masonry ,但是无论如何您都需要对自动布局的工作方式有一些了解。

Is it suggested that I actually use interface builder instead? 是否建议我实际上使用接口构建器?

This could be a highly opinionated issue. 这可能是一个自以为是的问题。 It depends entirely on whether you prefer using Interface Builder or not. 这完全取决于您是否愿意使用Interface Builder。 IB makes possible setting the constraints visually, but it is still not a trivial task, especially if your UI is a complex one. IB使可视化设置约束成为可能,但这仍然不是一件容易的事,特别是如果您的UI是一个复杂的UI。 If you do it programmatically, you will have more code (and ugly code, at that). 如果您以编程方式执行此操作,那么您将拥有更多的代码(和那样丑陋的代码)。 IB will also not fix "conflicts" between constraints, but it will make easier testing your constraints. IB也不会解决约束之间的“冲突”,但是它将使测试约束更加容易。 But if you prefer defining things visually, vs. programmatically, IB is a good choice. 但是,如果您希望以视觉方式而非程序方式定义事物,那么IB是一个不错的选择。

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

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