简体   繁体   中英

UITextView inside UIScrollView

I have a horizontal paging UIScrollView set up to have two pages. 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. 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. 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). setInfoPager is the horizontal paging scrollview, and is a subview of the cells 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垂直跳动?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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