2017-09-11 15 views
0

scrollView에는 Top에 UIImageView가 있고, UITextView에는 scrollingEnabled = NO가 포함되어 있습니다. 입력 한 위치에서 scrollView를 스크롤하고 싶습니다.TextView로 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 height는 사용자 유형에 따라 높이고 _contentView의 bottom은 textView의 bottom과 같습니다.

+0

사용하는 lib에 여러 LIB가 –

답변

1

사용 :
포드 'TPKeyboardAvoiding'또는
https://github.com/michaeltyson/TPKeyboardAvoiding

포드 'AnimatedTextInput'
나는 여러 게시물과 블로그를 통해 갈 https://github.com/jobandtalent/AnimatedTextInput

+1

나는 또한 내 프로젝트에이 포드를 사용 https://github.com/michaeltyson/TPKeyboardAvoiding 같은 동일한 사용할 수 있습니다이 내 시야를 자동 스크롤에 번거 로움을 많이 제거하고있다 키보드가 나타나면 조정 –

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]; 
    } 
} 
}