ios – 具有粘性页脚UIScrollView UIView和动态高度内容

ios – 具有粘性页脚UIScrollView UIView和动态高度内容,第1张

概述挑战时间! 想像一下我们有2个内容视图: > UIView具有动态高度内容(可扩展UITextView)= RED > UIView作为页脚= BLUE 该内容在UIScrollView = GEEN内 我应该如何使用自动布局来构建和处理约束以归档所有以下情况? 我正在考虑下一个基本结构: - UIScrollView (with always bounce vertically) - U 挑战时间!

想像一下我们有2个内容视图:

> UIVIEw具有动态高度内容(可扩展UITextVIEw)= RED
> UIVIEw作为页脚= BLUE

该内容在UIScrollVIEw = GEEN内

我应该如何使用自动布局来构建和处理约束以归档所有以下情况?

我正在考虑下一个基本结构:

- UIScrollVIEw (with always bounce vertically)    - UIVIEw - Container       - UIVIEw - DynamicHeightContent       - UIVIEw - Sticky Footer

键盘处理应该通过代码监视UIKeyboarDWillShowNotification和UIKeyboarDWillHIDeNotification来完成.我们可以选择将键盘的端框高度设置为Container UIVIEw底部引脚约束或UIScrollVIEw底部contentInset.

现在,棘手的部分是粘性页脚.

>如果有超过整个集装箱视图的屏幕可用,我们如何确保粘性页脚UIVIEw保持在底部?
>当键盘显示/隐藏时,我们如何知道可用的屏幕空间?我们一定会需要它
>这个结构是正确的吗?

谢谢.

解决方法 当UITextVIEw的文本内容相对较短时,内容视图的子视图(即文本视图和页脚)将无法通过约束来规定其内容视图的大小.这是因为当文本内容较短时,内容视图的大小将需要由滚动视图的大小决定.

更新:后一段是不真实的.您可以在内容视图本身或内容视图的视图层次结构中的某个位置安装固定高度约束.固定高度约束的常数可以在代码中设置,以反映滚动视图的高度.后一段也反映了思想的谬误.在纯粹的自动布局方法中,内容视图的子视图不需要规定滚动视图的contentSize;相反,它的内容视图本身最终必须指定contentSize.

无论如何,我决定和苹果公司所谓的“混合方式”一起使用UIScrollVIEw自动布局(参见苹果技术说明:https://developer.apple.com/library/ios/technotes/tn2154/_index.html)

一些iOS技术作家,如Erica Sadun,喜欢在几乎所有情况下使用混合方法(“iOS auto Layout DemystifIEd”,第2版).

在混合方法中,内容视图的框架和滚动视图的内容大小在代码中被明确设置.

这是我为这个挑战创建的GitHub回购:https://github.com/bilobatum/StickyFooterAutoLayoutChallenge.这是一个完整的布局更改动画的工作解决方案.它适用于不同尺寸的设备.为了简单起见,我禁止轮到风景.

对于那些不想下载和运行GitHub项目的人,我已经在下面列出了一些亮点(为了完整的实现,你必须看GitHub项目):

内容视图为橙色,文本视图为灰色,粘性页脚为蓝色.滚动时,状态栏后面会显示文本.我实际上并不喜欢,但是演示就好了.

在故事板中实例化的唯一视图是滚动视图,它是全屏(即,底部状态栏).

为了测试目的,我将一个双击手势识别器附加到蓝色的页脚,目的是解除键盘.

- (voID)vIEwDIDLoad{    [super vIEwDIDLoad];    self.scrollVIEw.alwaysBounceVertical = YES;    [self.scrollVIEw addSubvIEw:self.contentVIEw];    [self.contentVIEw addSubvIEw:self.textVIEw];    [self.contentVIEw addSubvIEw:self.stickyFooterVIEw];    [self configureConstraintsForContentVIEwSubvIEws];    // Apple's mixed (a.k.a. hybrID) approach to laying out a scroll vIEw with auto Layout: explicitly set content vIEw's frame and scroll vIEw's contentSize (see Apple's Technical Note TN2154: https://developer.apple.com/library/ios/technotes/tn2154/_index.HTML)    CGfloat textVIEwHeight = [self calculateHeightForTextVIEwWithString:self.textVIEw.text];    CGfloat contentVIEwHeight = [self calculateHeightForContentVIEwWithTextVIEwHeight:textVIEwHeight];    // scroll vIEw is fullscreen in storyboard; i.e.,it's final on-screen geometrIEs will be the same as the vIEw controller's main vIEw; unfortunately,the scroll vIEw's final on-screen geometrIEs are not available in vIEwDIDLoad    CGSize scrollVIEwSize = self.vIEw.bounds.size;    if (contentVIEwHeight < scrollVIEwSize.height) {        self.contentVIEw.frame = CGRectMake(0,scrollVIEwSize.wIDth,scrollVIEwSize.height);    } else {        self.contentVIEw.frame = CGRectMake(0,contentVIEwHeight);    }    self.scrollVIEw.contentSize = self.contentVIEw.bounds.size;}- (voID)configureConstraintsForContentVIEwSubvIEws{    assert(_textVIEw && _stickyFooterVIEw); // for deBUGging    // note: there is no constraint between the subvIEws along the vertical axis; the amount of vertical space between the subvIEws is determined by the content vIEw's height    Nsstring *format = @"H:|-(space)-[textVIEw]-(space)-|";    [self.contentVIEw addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:@{@"space": @(SIDE_margin)} vIEws:@{@"textVIEw": _textVIEw}]];    format = @"H:|-(space)-[footer]-(space)-|";    [self.contentVIEw addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:@{@"space": @(SIDE_margin)} vIEws:@{@"footer": _stickyFooterVIEw}]];    format = @"V:|-(space)-[textVIEw]";    [self.contentVIEw addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:@{@"space": @(top_margin)} vIEws:@{@"textVIEw": _textVIEw}]];    format = @"V:[footer(height)]-(space)-|";    [self.contentVIEw addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:@{@"space": @(BottOM_margin),@"height": @(FOOTER_HEIGHT)} vIEws:@{@"footer": _stickyFooterVIEw}]];    // a UITextVIEw does not have an intrinsic content size; will need to install an explicit height constraint based on the size of the text; when the text is modifIEd,this height constraint's constant will need to be updated    CGfloat textVIEwHeight = [self calculateHeightForTextVIEwWithString:self.textVIEw.text];    self.textVIEwHeightConstraint = [NSLayoutConstraint constraintWithItem:self.textVIEw attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplIEr:1.0f constant:textVIEwHeight];    [self.textVIEw addConstraint:self.textVIEwHeightConstraint];}- (voID)keyboardUp:(NSNotification *)notification{    // when the keyboard appears,extraneous vertical space between the subvIEws is eliminated–if necessary; i.e.,vertical space between the subvIEws is reduced to the minimum if this space is not already at the minimum    NSDictionary *info = [notification userInfo];    CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];    keyboardRect = [self.vIEw convertRect:keyboardRect fromVIEw:nil];    double duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];    CGfloat contentVIEwHeight = [self calculateHeightForContentVIEwWithTextVIEwHeight:self.textVIEw.bounds.size.height];    CGSize scrollVIEwSize = self.scrollVIEw.bounds.size;    [UIVIEw animateWithDuration:duration animations:^{        self.contentVIEw.frame = CGRectMake(0,contentVIEwHeight);        self.scrollVIEw.contentSize = self.contentVIEw.bounds.size;        UIEdgeInsets insets = UIEdgeInsetsMake(0,keyboardRect.size.height,0);        self.scrollVIEw.contentInset = insets;        self.scrollVIEw.scrollindicatorInsets = insets;        [self.vIEw layoutIfNeeded];    } completion:^(BOol finished) {        [self scrollToCaret];    }];}

虽然此演示应用程序的自动布局组件需要花费一些时间,但我花了几乎相当多的时间来滚动与嵌套在UIScrollVIEw内的UITextVIEw相关的问题.

总结

以上是内存溢出为你收集整理的ios – 具有粘性页脚UIScrollView UIView和动态高度内容全部内容,希望文章能够帮你解决ios – 具有粘性页脚UIScrollView UIView和动态高度内容所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1097701.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存