简体   繁体   English

如何在Xcode中成功使用AutoLayout和Constraints?

[英]How do I successfully use AutoLayout and Constraints in Xcode?

This is going to be difficult without the rep needed to post images... however... 没有发布图片所需的代表,这将非常困难...但是...

I currently have a storyboard layout in Xcode. 我目前在Xcode中有一个分镜脚本布局。

I have a UIViewController which is split in two sections, the upper UIView, and the lower UITableView (For now, we can ignore this, however the same issue applies to the table view, and advice is also appreciated) 我有一个UIViewController,它分为两个部分,上部的UIView和下部的UITableView(目前,我们可以忽略它,但是相同的问题也适用于表视图,建议也应多加注意)

In the UIView, I have a ProgressView, which spans the entire width of the UIView, and is snapped to the padding distance from the edge of the view. 在UIView中,我有一个ProgressView,它跨过UIView的整个宽度,并且捕捉到距视图边缘的填充距离。 The ProgressView is set to 50% and falls in the middle of the layout in the storyboard designer. ProgressView设置为50%,并且位于情节提要设计器中布局的中间。

When this is simulated, the progress bar appears to be sitting well above 90%. 模拟时,进度条似乎位于90%以上。 Issue is, when the progress bar is shrunk (or the width changed in the storyboard layout editor) to about half the 'visible layout', the progress bar takes up 'most' of the simulated screen, and correctly displays the 50% progress. 问题是,当进度条缩小(或在情节提要布局编辑器中更改宽度)到“可见布局”的一半时,进度条将占用模拟屏幕的“大部分”,并正确显示进度的50%。

I'm assuming this has something to do with the AutoLayout or Size Classes. 我假设这与AutoLayout或Size类有关。 I'd like to learn the use of this autolayout and constraints properly - as such, disabling either of these will not be acceptable. 我想正确地学习如何使用此自动版式和约束-因此,禁用其中任何一个都是不可接受的。 The suggested constraints are not returning my expected results. 建议的约束没有返回我的预期结果。

Edit: I've figured out something that seems to work satisfactorily, see the given answer. 编辑:我想出了一些似乎令人满意的工作,请参阅给定的答案。 But still keen to see some actual layout advice. 但是仍然渴望看到一些实际的布局建议。

Edit: Applying the same principle to the UITableView in the lower half of the UIViewController and it also fixes the problems with Separator Insets. 编辑:将相同的原理应用于UIViewController下半部分的UITableView,它还解决了分隔符插入的问题。

I have found one solution. 我找到了一种解决方案。

In storyboard editor, open the document outline. 在情节提要编辑器中,打开文档大纲。 Hold rightclick and select the ProgressView and drag to the Parent UIView, release and select: 右键单击并选择ProgressView,然后拖动到父UIView,然后释放并选择:

  • Leading Space to Container 通往集装箱的领先空间
  • Trailing Space to Container 拖到集装箱的空间
  • Center Horizontally in Container 在容器中水平居中

(Finishing here still has issues because the UIView exceeds the width of its parent still.... so do the same to the UIView) (这里的完成仍然存在问题,因为UIView仍然超过其父级的宽度...。对UIView也是一样的)

In the document outline, hold right click on the UIView and drag to the Parent View, release and select: - Leading Space to Container - Trailing Space to Container 在文档大纲中,右键单击UIView并拖动到父视图,然后释放并选择:-将空间引导到容器-将空间拖到容器

Now, I have a perfectly centered ProgressView with it sitting between the two edges and with the 'padding' on either end. 现在,我有了一个完美居中的ProgressView,它位于两个边缘之间,并且两端都带有“填充”。 Exactly what was required. 确实需要什么。

This issue seems to have spanned from utilising nested UIViews. 这个问题似乎已经从利用嵌套的UIView跨越了。

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

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