我有滚动视图在顶部包含一个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的底部而增加。
答案 0 :(得分:1)
使用:
pod' TPKeyboardAvoiding '或
https://github.com/michaeltyson/TPKeyboardAvoiding
pod' AnimatedTextInput '
https://github.com/jobandtalent/AnimatedTextInput
答案 1 :(得分:0)
我已经浏览了几个帖子和博客,但我没有得到我想要的确切答案。在那之后,我在其他帖子和博客的帮助下想出了某种解决方案。希望这会有所帮助。
- (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];
}
}
}