웰코발
웰코's iOS
웰코발
전체 방문자
오늘
어제
  • 분류 전체보기 (63)
    • Swift (26)
    • rxSwift (13)
    • SwiftUI (3)
    • iOS (12)
    • 기타 (1)
    • 개발관련 용어정리 (6)
    • 면접준비 (0)
    • 공공데이터 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • SWIFT
  • Observable
  • 대기오염통계 현황
  • WKWebView
  • ios
  • 주제구독
  • Scroll
  • UI
  • 디자인
  • collectionview
  • ReactorKit
  • delay
  • uitableview
  • content_available
  • cell
  • swiftUI
  • alamofire
  • Coordinator
  • 측정소정보
  • rxswift

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
웰코발

웰코's iOS

Swift

[Swift] UIScrollView 내 UITextView 의 Cursor 로 focus scroll 하는 방법

2022. 11. 17. 20:05

UIScrollView 안에 UITextView를 넣고 textView에 포커싱을 올리면

키보드에 가려져 스크롤을 해야하는 번거로움이 있다.

그럴경우 아래와 같은 코드를 사용해보자.


// UITextViewDelegate 내 override 함수에 활용

func textViewDidChange(_ textView: UITextView) {

    let deadlineTime = DispatchTime.now() + .milliseconds(100)

    DispatchQueue.main.asyncAfter(deadline: deadlineTime) { 
        if let cursorPosition = textView.selectedTextRange?.end {

            let caretPositionRect = textView.caretRect(for: cursorPosition)
            print(caretPositionRect, "caret")
            let pointsuperview = textView.convert(caretPositionRect, to: self.scrollView)
            self.scrollView.scrollRectToVisible(pointsuperview, animated: false)
        }
    }
    
    
}

textview의 마지막 글자의 커서 위치를 얻어 해당 위치로 스크롤 시키는 방법이다.

 

'Swift' 카테고리의 다른 글

[Swift] WebKit 의 웹 관련 캐시 삭제 하는 방법  (0) 2022.12.16
[Swift] Alamofire 통신에서 캐시 사용 안하는 방법  (0) 2022.11.25
[Swift] ScrollView 에서 Button Highlight Delay가 생길 때  (2) 2022.11.11
[Swift] CLLocationManager.locationServicesEnabled() Issue 문제  (2) 2022.11.02
[Swift] WebKit 의 WKWebView 사용법  (0) 2022.10.18
    'Swift' 카테고리의 다른 글
    • [Swift] WebKit 의 웹 관련 캐시 삭제 하는 방법
    • [Swift] Alamofire 통신에서 캐시 사용 안하는 방법
    • [Swift] ScrollView 에서 Button Highlight Delay가 생길 때
    • [Swift] CLLocationManager.locationServicesEnabled() Issue 문제
    웰코발
    웰코발
    나의 개발 일지

    티스토리툴바