UIScrollView 内的 UITextView
Posted
技术标签:
【中文标题】UIScrollView 内的 UITextView【英文标题】:UITextView inside UIScrollView 【发布时间】:2014-07-20 04:02:40 【问题描述】:我有一个水平分页 UIScrollView 设置有两个页面。在滚动视图内部有一个“内容视图”,它设置了约束,因此它是滚动视图的“内容大小”。内容视图包含两个子视图,第一页和第二页。这工作正常,我可以在两页之间水平翻页。没有垂直“反弹”,因为页面完全填满了内容大小。
我还没有向第一页添加任何内容,但正在向第二页添加 UITextView。此文本视图设置了约束,使其位于第二页的中心,并适合第二页。目的是这只会显示一个简短的简介,因此对于文本视图,scrollEnabled 设置为 NO。文本应该很容易适应空间而不会被截断。
我在文本视图中放置了一些虚拟文本,一切正常。然后我稍微增加了字体。虽然文本仍然很容易放入允许的空间内,但我现在在滚动视图上收到一个垂直“反弹” - 就好像内容大小现在垂直更大(或插图已更改)。我已经检查了这些,它们仍然与字体更改之前完全相同。文本视图的内在内容大小略大(因为增加了字体大小),但同样在约束范围内。是什么导致垂直反弹?
这是设置视图和约束的代码。请注意,这是在表格视图单元格 (c) 内。 setInfoPager 是水平分页滚动视图,是单元格 contentView 的子视图。
UIView *contentView = [[UIView alloc] init];
contentView.translatesAutoresizingMaskIntoConstraints = NO;
[c.setInfoPager addSubview:contentView];
[c.setInfoPager addConstraint:[NSLayoutConstraint constraintWithItem:contentView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:c.setInfoPager
attribute:NSLayoutAttributeRight
multiplier:1.f
constant:0.f]];
[c.setInfoPager addConstraint:[NSLayoutConstraint constraintWithItem:contentView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:c.setInfoPager
attribute:NSLayoutAttributeLeft
multiplier:1.f
constant:0.f]];
[c.setInfoPager addConstraint:[NSLayoutConstraint constraintWithItem:contentView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:c.setInfoPager
attribute:NSLayoutAttributeTop
multiplier:1.f
constant:0.f]];
[c.setInfoPager addConstraint:[NSLayoutConstraint constraintWithItem:contentView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:c.setInfoPager
attribute:NSLayoutAttributeBottom
multiplier:1.f
constant:0.f]];
[c.setInfoPager addConstraint:[NSLayoutConstraint constraintWithItem:contentView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:c.setInfoPager
attribute:NSLayoutAttributeWidth
multiplier:2.0
constant:0.f]];
[c.setInfoPager addConstraint:[NSLayoutConstraint constraintWithItem:contentView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:c.setInfoPager
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0.f]];
UIView *pageOne = [[UIView alloc] init];
pageOne.translatesAutoresizingMaskIntoConstraints = NO;
[contentView addSubview:pageOne];
[contentView addConstraint:[NSLayoutConstraint constraintWithItem:pageOne
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeHeight
multiplier:1.f
constant:0.f]];
[contentView addConstraint:[NSLayoutConstraint constraintWithItem:pageOne
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeWidth
multiplier:0.5f
constant:0.f]];
[contentView addConstraint:[NSLayoutConstraint constraintWithItem:pageOne
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeLeft
multiplier:1.f
constant:0.f]];
[contentView addConstraint:[NSLayoutConstraint constraintWithItem:pageOne
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeTop
multiplier:1.f
constant:0.f]];
UIView *pageTwo = [[UIView alloc] init];
pageTwo.translatesAutoresizingMaskIntoConstraints = NO;
pageTwo.backgroundColor = [UIColor greenColor];
[contentView addSubview:pageTwo];
[contentView addConstraint:[NSLayoutConstraint constraintWithItem:pageTwo
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeHeight
multiplier:1.f
constant:0.f]];
[contentView addConstraint:[NSLayoutConstraint constraintWithItem:pageTwo
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeWidth
multiplier:0.5f
constant:0.f]];
[contentView addConstraint:[NSLayoutConstraint constraintWithItem:pageTwo
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeRight
multiplier:1.f
constant:0.f]];
[contentView addConstraint:[NSLayoutConstraint constraintWithItem:pageTwo
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeTop
multiplier:1.f
constant:0.f]];
UITextView *cardSetBlurb = [[UITextView alloc] init];
cardSetBlurb.scrollEnabled = NO;
cardSetBlurb.translatesAutoresizingMaskIntoConstraints = NO;
[pageTwo addSubview:cardSetBlurb];
cardSetBlurb.backgroundColor = [UIColor blackColor];
cardSetBlurb.textColor = [UIColor whiteColor];
cardSetBlurb.userInteractionEnabled = NO;
cardSetBlurb.font = [UIFont fontWithName:@"AvenirNext-Regular" size:12.f];
cardSetBlurb.text = @"blah blah blah sdfasdf dsfasdf asdfasdf asdfasdf asdfasdf asdfasdf asdfasdf something wicked this way comes";
[pageTwo addConstraint:[NSLayoutConstraint constraintWithItem:cardSetBlurb
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:pageTwo
attribute:NSLayoutAttributeLeft
multiplier:1.f
constant:0.f]];
[pageTwo addConstraint:[NSLayoutConstraint constraintWithItem:cardSetBlurb
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:pageTwo
attribute:NSLayoutAttributeTop
multiplier:1.f
constant:0.f]];
[pageTwo addConstraint:[NSLayoutConstraint constraintWithItem:cardSetBlurb
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:pageTwo
attribute:NSLayoutAttributeRight
multiplier:1.f
constant:0.f]];
[pageTwo addConstraint:[NSLayoutConstraint constraintWithItem:cardSetBlurb
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:pageTwo
attribute:NSLayoutAttributeBottom
multiplier:1.f
constant:0.f]];
[pageTwo addConstraint:[NSLayoutConstraint constraintWithItem:cardSetBlurb
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:pageTwo
attribute:NSLayoutAttributeCenterX
multiplier:1.f
constant:0.f]];
[pageTwo addConstraint:[NSLayoutConstraint constraintWithItem:cardSetBlurb
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:pageTwo
attribute:NSLayoutAttributeCenterY
multiplier:1.f
constant:0.f]];
【问题讨论】:
【参考方案1】:您是否尝试过在界面构建器中简单地停止故事板的垂直弹跳?
【讨论】:
以上是关于UIScrollView 内的 UITextView的主要内容,如果未能解决你的问题,请参考以下文章
分页 UIScrollView 内的 UIScrollView
限制 UIScrollView 内的 UILabel 的高度
UIScrollview 内的 UIView 内的 UILabel 上的 UIGestureRecognizer
UIScrollview内的UIView内的UIButton,ARC问题
Swift - 通过点击 ScrollView 内的 ViewController 内的按钮来滚动 UIScrollView