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

前端之家收集整理的这篇文章主要介绍了ios – 具有粘性页脚UIScrollView UIView和动态高度内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_0@
挑战时间!

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

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

内容在UIScrollView = GEEN内

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

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

  1. - UIScrollView (with always bounce vertically)
  2. - UIView - Container
  3. - UIView - DynamicHeightContent
  4. - 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项目):

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

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

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

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4.  
  5. self.scrollView.alwaysBounceVertical = YES;
  6.  
  7. [self.scrollView addSubview:self.contentView];
  8. [self.contentView addSubview:self.textView];
  9. [self.contentView addSubview:self.stickyFooterView];
  10.  
  11. [self configureConstraintsForContentViewSubviews];
  12.  
  13. // 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)
  14. CGFloat textViewHeight = [self calculateHeightForTextViewWithString:self.textView.text];
  15. CGFloat contentViewHeight = [self calculateHeightForContentViewWithTextViewHeight:textViewHeight];
  16. // 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
  17. CGSize scrollViewSize = self.view.bounds.size;
  18.  
  19. if (contentViewHeight < scrollViewSize.height) {
  20. self.contentView.frame = CGRectMake(0,scrollViewSize.width,scrollViewSize.height);
  21. } else {
  22. self.contentView.frame = CGRectMake(0,contentViewHeight);
  23. }
  24.  
  25. self.scrollView.contentSize = self.contentView.bounds.size;
  26. }
  27.  
  28. - (void)configureConstraintsForContentViewSubviews
  29. {
  30. assert(_textView && _stickyFooterView); // for debugging
  31.  
  32. // 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
  33.  
  34. NSString *format = @"H:|-(space)-[textView]-(space)-|";
  35. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:@{@"space": @(SIDE_MARGIN)} views:@{@"textView": _textView}]];
  36.  
  37. format = @"H:|-(space)-[footer]-(space)-|";
  38. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:@{@"space": @(SIDE_MARGIN)} views:@{@"footer": _stickyFooterView}]];
  39.  
  40. format = @"V:|-(space)-[textView]";
  41. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:@{@"space": @(TOP_MARGIN)} views:@{@"textView": _textView}]];
  42.  
  43. format = @"V:[footer(height)]-(space)-|";
  44. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:@{@"space": @(BOTTOM_MARGIN),@"height": @(FOOTER_HEIGHT)} views:@{@"footer": _stickyFooterView}]];
  45.  
  46. // 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
  47. CGFloat textViewHeight = [self calculateHeightForTextViewWithString:self.textView.text];
  48.  
  49. self.textViewHeightConstraint = [NSLayoutConstraint constraintWithItem:self.textView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0f constant:textViewHeight];
  50.  
  51. [self.textView addConstraint:self.textViewHeightConstraint];
  52. }
  53.  
  54. - (void)keyboardUp:(NSNotification *)notification
  55. {
  56. // 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
  57.  
  58. NSDictionary *info = [notification userInfo];
  59. CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  60. keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
  61. double duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  62.  
  63. CGFloat contentViewHeight = [self calculateHeightForContentViewWithTextViewHeight:self.textView.bounds.size.height];
  64. CGSize scrollViewSize = self.scrollView.bounds.size;
  65.  
  66. [UIView animateWithDuration:duration animations:^{
  67.  
  68. self.contentView.frame = CGRectMake(0,contentViewHeight);
  69. self.scrollView.contentSize = self.contentView.bounds.size;
  70. UIEdgeInsets insets = UIEdgeInsetsMake(0,keyboardRect.size.height,0);
  71. self.scrollView.contentInset = insets;
  72. self.scrollView.scrollIndicatorInsets = insets;
  73.  
  74. [self.view layoutIfNeeded];
  75.  
  76. } completion:^(BOOL finished) {
  77.  
  78. [self scrollToCaret];
  79. }];
  80. }

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

猜你在找的iOS相关文章