【问题标题】:Scrolling of scrollView with TextView使用 TextView 滚动 scrollView
【发布时间】:2018-02-19 08:05:46
【问题描述】:

我的 scrollView 在 Top 上包含一个 UIImageView 和一个 UITextView 并且 scrollingEnabled = NO ,我想在我输入的位置滚动scrollView。

- (void)createScrollView{
//TPKeyboardAvoidingScrollView *scrollView = [[TPKeyboardAvoidingScrollView alloc]init];
UIScrollView *scrollView = [[UIScrollView alloc]init];
//[self.view insertSubview:scrollView belowSubview:_mediaSelectionView];
[self.view addSubview: scrollView];
[scrollView setTranslatesAutoresizingMaskIntoConstraints: NO];
self.scrollView = scrollView;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.bouncesZoom = NO;
scrollView.alwaysBounceVertical = YES;
scrollView.clipsToBounds = YES;
self.automaticallyAdjustsScrollViewInsets = YES;
NSLayoutConstraint *scrollViewTop = [NSLayoutConstraint
                                     constraintWithItem: scrollView
                                     attribute: NSLayoutAttributeTop
                                     relatedBy: NSLayoutRelationEqual
                                     toItem: self.navigationBarBGView
                                     attribute: NSLayoutAttributeBottom
                                     multiplier: 1 constant:0.0
                                     ];
NSLayoutConstraint *scrollViewLeading = [NSLayoutConstraint
                                         constraintWithItem: scrollView
                                         attribute: NSLayoutAttributeLeading
                                         relatedBy: NSLayoutRelationEqual
                                         toItem: self.view
                                         attribute: NSLayoutAttributeLeading
                                         multiplier: 1 constant:0.0
                                         ];

NSLayoutConstraint *superViewTraling = [NSLayoutConstraint
                                        constraintWithItem: self.view
                                        attribute: NSLayoutAttributeTrailing
                                        relatedBy: NSLayoutRelationEqual
                                        toItem: scrollView
                                        attribute: NSLayoutAttributeTrailing
                                        multiplier: 1 constant:0.0
                                        ];

NSLayoutConstraint *bottomLayoutGuideTop = [NSLayoutConstraint
                                            constraintWithItem:self.view
                                            attribute: NSLayoutAttributeBottom
                                            relatedBy: NSLayoutRelationEqual
                                            toItem: scrollView
                                            attribute: NSLayoutAttributeBottom
                                            multiplier: 1 constant:0.0
                                            ];
//Add All Constrains.
[self.view addConstraints: @[scrollViewTop , scrollViewLeading , superViewTraling , bottomLayoutGuideTop ]];

_contentView = [[UIView alloc]init];

[scrollView addSubview: _contentView];
[_contentView setConstraintFlag];
[_contentView setFullWidth];
[_contentView setTopFromParent:0];

}

- (void)createCommentTextView{
    UITextView *textView = [[UITextView alloc]init];
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor colorWithR:67 G:83 B:83 A:1.0f];
    textView.delegate = self;
    textView.scrollEnabled = NO;        
    _commentTextView = textView;
    [_textViewContainer addSubview:textView];

}

-(void)updateContentSize{
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, self.contentView.frame.size.height);

}

scrollView 包含 _contentView 并且 contentView 包含 UITextView。 textView 高度随着用户类型而增加,_contentView 的底部等于 textView 的底部。

【问题讨论】:

标签: ios uiscrollview uitextview vertical-scrolling


【解决方案1】:

我浏览了几篇文章和博客,但没有得到我想要的确切答案。在那之后,我在其他帖子和博客的帮助下想出了某种解决方案。希望这会有所帮助。

- (void)scrollToCursor{
    if(self.currentTextView.selectedRange.location != NSNotFound) {

    NSRange range = self.currentTextView.selectedRange;

    NSString *string = [<YOUR_TEXTVIEW>.text substringToIndex:range.location];

    CGSize size = [string boundingRectWithSize:<YOUR_TEXTVIEW>.frame.size
                                       options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading
                                    attributes:@{NSFontAttributeName:<YOUR_TEXTVIEW>.font}
                                       context:nil].size;

    NSInteger yPosition = (<YOUR_TEXTVIEW>.frame.origin.y+size.height+<OFFSET(if required)>);


    NSInteger scrollViewVisibeYPositionStart = self.scrollView.contentOffset.y;
    NSInteger scrollViewVisibeYPositionEnd = scrollViewVisibeYPositionStart+self.scrollView.frame.size.height;

    BOOL needToSetOffset = NO;

    if (yPosition > scrollViewVisibeYPositionEnd) {
        yPosition = yPosition - self.scrollView.frame.size.height;
        needToSetOffset = YES;

    }else if (yPosition < scrollViewVisibeYPositionStart){
        yPosition = self.scrollView.frame.size.height + (scrollViewVisibeYPositionStart - yPosition);
        needToSetOffset = YES;
    }

    if (needToSetOffset) {
        CGPoint offset = CGPointMake(0,yPosition);
        [self.scrollView setContentOffset:offset];
  }
}
}

【讨论】:

    【解决方案2】:

    【讨论】:

    • 我在我的项目中也使用了这些 Pod,这消除了自动滚动我的视图和在键盘出现时进行调整的很多麻烦
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 2023-04-05
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多