简体   繁体   English

UIScrollView内的UITextView

[英]UITextView inside UIScrollView

I have a horizontal paging UIScrollView set up to have two pages. 我将水平分页UIScrollView设置为具有两个页面。 Inside the scrollview there is a "content view" which has constraints set so that it is the "content size" of the scrollview. 在滚动视图内部有一个“内容视图”,它具有一些约束条件,因此它是滚动视图的“内容大小”。 The content view contains two sub views, page one and page two. 内容视图包含两个子视图,第一页和第二页。 This is working fine, and I can page horizontally between the two pages. 一切正常,我可以在两页之间水平翻页。 There is no vertical "bounce" because the pages exactly fill the content size. 没有垂直的“反弹”,因为页面恰好填满了内容大小。

I haven't added anything to the first page yet, but am adding a UITextView to the second page. 我还没有将任何内容添加到第一页,但是正在将UITextView添加到第二页。 This text view has constraints set so that it is centered in page two, and fits within page two. 此文本视图已设置约束,因此它位于第二页的中心,并且适合第二页。 The intention is that this will just show a short blurb, so scrollEnabled is set to NO for the text view. 目的是仅显示简短的Blurb,因此文本视图的scrollEnabled设置为NO。 The text should easily fit within the space without being cut off. 文本应容易地适合空间而不被截断。

I placed some dummy text in the text view and everything worked fine. 我在文本视图中放置了一些虚拟文本,并且一切正常。 I then increased the font slightly. 然后,我稍微增加了字体。 While the text still easily fits inside the space allowed, I now receive a vertical "bounce" on the scrollview - its as if the content size is now vertically larger (or the insets have changed). 尽管文本仍然可以轻松地容纳在允许的空间内,但我现在在滚动视图上看到了一个垂直的“反弹”-好像内容大小现在在垂直方向上变大了(或插图已更改)。 I've examined these and they are still exactly the same as they were before the font change. 我已经检查了这些,它们仍然与更改字体之前完全相同。 The intrinsic content size of the text view is slightly bigger (because of the increased font size), but again well within the constraints. 文本视图的固有内容大小稍大一些(由于增加了字体大小),但仍在限制范围内。 What is causing the vertical bounce? 是什么引起垂直反弹?

Here is the code setting up the views and constraints. 这是设置视图和约束的代码。 Note that this is within a table view cell (c). 请注意,这在表视图单元格(c)中。 setInfoPager is the horizontal paging scrollview, and is a subview of the cells contentView. 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]];

您是否尝试过仅在界面生成器中停止从Storyboard垂直跳动?

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

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