简体   繁体   English

在xcode8中的iOS 10.2的scrollview中为UIimageView和textview进行自动布局

[英]making an auto layout of UIimageView and textview inside scrollview for iOS 10.2 in xcode8

I am making a note taking app in xcode8 for iOS 10.2. 我正在使用iOS 10.2的xcode8做笔记应用程序。 The note is consist of a textfield, textview and imageview. 注释由文本字段,文本视图和图像视图组成。 I am adding textview and imageview in separate scrollviews (to provide the facility of zooming and adjusting image). 我在单独的滚动视图中添加了textview和imageview(以提供缩放和调整图像的功能)。 Then I have added both scroll views in a stackview to ensure that imageview and textview stands side by side in a landscape mode. 然后,我在stackview中添加了两个滚动视图,以确保imageview和textview在横向模式下并排站立。 To do that, I have added variation to axis property of stackview( width=any, height= compact and Gamut=any, and set that to the horizontal, as shown in right corner of screenshot to see view hierarchy without any constraints ). 为此,我向stackview的axis属性添加了变化(width = any,height = compact和Gamut = any,并将其设置为水平,如屏幕快照的右上角所示, 以查看没有任何约束的视图层次结构 )。 I have tried many constraints but, every time it fails to adopt. 我尝试了许多限制,但是每次都无法采用时。 Let's take an example, I am adjusting size of the scrollviews on the size of content(by setting top,bottom,leading and trailing constraint), the stack view is adjusted to the main view. 让我们举个例子,我正在根据内容的大小调整滚动视图的大小(通过设置顶部,底部,前导和尾随约束),将堆栈视图调整为主视图。 Now, the only thing that needs to be adjusted is text view and imageview with respect of stack view. 现在,唯一需要调整的是相对于堆栈视图的文本视图和图像视图。 But, here is the tricky part If I include a fixed trailing constraint of text view to the stack view; 但是,这是一个棘手的部分,如果我在堆栈视图中包括固定的文本视图尾随约束; it will work in portrait mode.As soon I turn into landscape mode that constraint will not work(the trailing constraint which have 0 distance from stackview) because the edge will be side to the imageview not to the stackview(because I am putting image and text side by side as explained above) which will make textview larger than required, same goes for imageview's leading constraint to the stack view and vertical distance constraint betweem image view and textview.I think the root of the problem is I have made a variation to the axis of a stack view (in order to get text and image side by side in landscape mode) but, that was necessary part.Can anyone have solution for this? 一旦进入横向模式,约束将不起作用(尾随约束与stackview的距离为0),因为边缘将位于imageview的一侧而不是stackview(因为我将image和文本并排(如上所述),这将使textview大于所需的大小,imageview对堆栈视图的领先约束以及在view view和textview之间的垂直距离约束也是如此。我认为问题的根源在于我对堆栈视图的轴(以便在横向模式下并排获取文本和图像),但这是必不可少的部分。有人对此有解决方案吗? Do you think there can be other problem? 您认为还有其他问题吗? Should I choose Intrinsic Size of scrollviews to placeholder option or let it be system default? 我应该选择“滚动视图到占位符的本征大小”选项还是将其设置为系统默认值?

Thank you. 谢谢。

So I hope I understand the question and I will try to walk you through what I understand you hope to accomplish. 因此,我希望我理解这个问题,并且将尝试引导您完成我理解您希望完成的工作。 I preface this by saying I just started using UIStackView because of backwards compatibility. 首先,我说这是因为向后兼容,所以我才开始使用UIStackView。

Note. 注意。 To get the magic of ScrollView with AutoLayout I almost always embed them in another UIView. 为了使用AutoLayout获得ScrollView的魔力,我几乎总是将它们嵌入另一个UIView中。 There are reasons to not but in this case you will see how valuable this is to AutoLayout. 有理由不这样做,但是在这种情况下,您将看到这对AutoLayout有多么重要。

Step 1) Drag your UIStackView and add Top,Bottom,Leading, and Trailing. 步骤1)拖动您的UIStackView并添加Top,Bottom,Leading和Trailing。 Now add 2 UIViews and set the UIStackView to Fill Equally. 现在添加2个UIView,并将UIStackView设置为“均等填充”。 It will now look like the image(Background colors to check your work). 现在它将看起来像图像(背景色可检查您的工作)。 StackViewStep1

Step 2) Now add a UIScrollView to each of these UIViews. 步骤2)现在向每个UIView添加一个UIScrollView。 Add Leading,Trailing,Top,and Bottom on each of these. 在每一个上添加Leading,Trailing,Top和Bottom。

Step 3) Add a UITextView and UIImageView to the ScrollViews respectively. 步骤3)将UITextView和UIImageView分别添加到ScrollViews。 Now Autolayout is mad at us :( but we will fix that. 现在,Autolayout对我们很生气:(但我们会解决这个问题。

Step 4) Drag from the UIImageView to the View that is holding the UIScrollView(First Set of Views we added). 步骤4)从UIImageView拖动到保存UIScrollView的View(我们添加的第一组View)。 See image 看图片

第四步

-Choose the option to add Equal Widths. -选择添加等宽选项。 See Image 见图片 EqualWidths

Repeat the same step but add Equal Heights. 重复相同的步骤,但添加等高。 Now add Top, Bottom, Leading, and Trailing to the ScrollView. 现在,将“顶部”,“底部”,“前导”和“尾随”添加到ScrollView。

Step 5) Repeat exact Step 4 with UITextView 步骤5)使用UITextView重复步骤4

Your final view hierarchy should look like this. 您的最终视图层次结构应如下所示。 最后

Now you can rotate your UIStackView and do what you want I think. 现在,您可以旋转UIStackView并按照我的想法去做。 StackVertical

And Horizontal 和水平 横

Side by Side Preview 并排预览 SideBySide

I did add a <= 0.1 equal heights multiplier on the textfield at the top but I don't know if that was necessary. 我确实在顶部的文本字段上添加了<= 0.1等高倍数,但我不知道这是否有必要。

Enjoy. 请享用。

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

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