简体   繁体   English

在ScrollView中使用动态UIView自动布局

[英]Autolayout with a dynamic UIView in a ScrollView

I'm using a mix of Xamarin and XCode Interface Builder to build my UI. 我混合使用Xamarin和XCode Interface Builder来构建UI。

As you can see in the screen shots the PlaceholderView at the top can have different content. 从屏幕快照中可以看到,顶部的PlaceholderView可以具有不同的内容。 The problem I'm having is trying to keep the Submit button at the bottom of the ContentView. 我遇到的问题是尝试将“提交”按钮保留在ContentView的底部。

查看A 查看B

I'm using the ScrollView with ContentView approach and setting constraints in IB. 我正在将ScrollView与ContentView方法一起使用,并在IB中设置约束。

In ViewDidLoad() I load the contents for the PlaceholderView and then I set the height constraint of the PlaceholderView programmatically. 在ViewDidLoad()中,我加载PlaceholderView的内容,然后以编程方式设置PlaceholderView的高度约束。

public override void ViewDidLoad()
{
   onlineSuspectDetails = OnlineSuspectDetailsView.Create();
   onlineSuspectDetails.BackgroundColor = UIColor.Gray;                    
   SuspectDetailsPlaceholderView.AddSubview(onlineSuspectDetails);
   SuspectDetailsPlaceholderView.HeightAnchor.ConstraintEqualTo(onlineSuspectDetails.HeightAnchor, 1).Active = true;
}

Now of course I had to set a Top and Bottom constraint for the Submit Button so it's working for one type but I can't see a way to change it depending on height of the PlaceholderView in order to keep the Submit Button at the bottom. 当然,现在我必须为Submit Button设置一个Top和Bottom约束,因此它只能用于一种类型,但是我看不到一种方法来根据PlaceholderView的高度来更改它,以便将Submit Button保持在底部。

If I could access the Bottom constraint I can calculate the new Top constraint but I can't find a way to access the Bottom constraint. 如果可以访问Bottom约束,则可以计算新的Top约束,但是找不到找到Bottom约束的方法。 How can I do this? 我怎样才能做到这一点?

Are there any alternative suggestions to how I can solve this problem? 关于如何解决此问题,是否还有其他建议?

Hmmm... a bit tricky... 嗯...有点棘手...

There may be better ways to do it, but here is one approach: 可能有更好的方法,但这是一种方法:

  • Orange = main view background 橙色=主视图背景
  • Pale Yellow = scroll view background 淡黄色=滚动视图背景
  • Gray = UIView ... "label / field pairs" container; 灰色= UIView ...“标签/字段对”容器; label/field pairs are in a standard UIStackView 标签/字段对在标准UIStackView
  • Cyan = UIView ... "details" container 青色= UIView ...“详细信息”容器
  • Dark Green = button 深绿色=按钮
  • Red = UIView ... this is the tricky part... is a "Shim" to hold the button at the bottom 红色= UIView ...这是棘手的部分...是将按钮保持在底部的“垫片”

在此处输入图片说明

View constraints are inset by 8 so we can see them easier than if they're taking up the full screen/view. 视图约束由8插入,因此与占据全屏/视图相比,我们可以更轻松地看到它们。

Gray view and Details view constraints for positions / sizes are straight-forward (looks like you have no problem with that aspect). 位置/大小的灰色视图和详细信息视图约束很简单(看起来您对此方面没有问题)。

In this method, we use a "Shim" view, and some greater-than-or-equal-to constraints to manage the Button's position. 在此方法中,我们使用“ Shim”视图和一些大于或等于的约束来管理Button的位置。

The Shim is pinned leading and top to Zero, and its Height constraint is set to >= -30 relative to the scroll view height. Shim固定在前导且顶部为零,并且其“高度”约束相对于滚动视图高度设置为>= -30 Its bottom constraint is also set to >= 8 relative to the bottom of the Details view. 相对于“详细信息”视图的底部 ,其底部约束也设置为>= 8

This tells auto-layout to put the bottom of the Shim no more than 30-pts from the bottom of the scroll view AND at least 8-pts below the bottom of the Details view. 这告诉自动布局从滚动视图的底部放垫片的底部不超过 30分详细的底部至少低于8分查看。

Then the top of the Submit button is constrained to the bottom of the Shim view. 然后,“提交”按钮的顶部被限制在“ Shim”视图的底部。

One "quirk" that I've found when working with scroll views in Interface Builder - it can be really tough (maybe impossible?) to get IB to be happy with the necessary constraints. 我在Interface Builder中使用滚动视图时发现的一个“怪癖”-让IB对必要的约束感到满意可能非常困难(也许不可能?)。 It will show conflicts, but if you follow IB's "fixes" the desired layout then fails. 它将显示冲突,但是如果遵循IB的“修复”,则所需的布局将失败。

I don't actually work with IB / Storyboards, so I just focus on avoiding auto-layout / constraint conflicts at runtime. 我实际上不使用IB /情节提要,因此我只专注于避免在运行时发生自动布局/约束冲突。

This is probably easier to understand by seeing the actual file, so I put this up as a GitHub repo: https://github.com/DonMag/SWMoreScrollViewStuff 通过查看实际文件,这可能更容易理解,因此我将其作为GitHub存储库放置: https : //github.com/DonMag/SWMoreScrollViewStuff

How are you actually adding the button to the main view? 您实际上如何将按钮添加到主视图? I have done something like this before by having a master UIView for everything except the button. 我通过对按钮以外的所有内容都拥有一个主UIView来完成类似的操作。 And then I just put the button below the view and applied auto layout to everything (should just be 0,0,0,0 on the view and button). 然后,我将按钮放在视图下方,并对所有内容应用自动布局(在视图和按钮上应仅为0,0,0,0)。 This way your button is always at the front of your view and you can do everything else in the contained view! 这样,您的按钮始终位于视图的前面,您可以在包含的视图中执行其他所有操作!

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

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