[RxCocoa] Notification

RudinP·4일 전
0

Study

목록 보기
373/375

옵저버를 추가하고 해제하는 작업이 자동으로 처리됨
여기서는 키보드 표시에 따른 텍스트뷰 조정 처리 작업

1. Notification 옵저버블 생성

		let willShowObservable = NotificationCenter.default.rx.notification(UIResponder.keyboardWillShowNotification)
            .map { ($0.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height ?? 0 }
        
        let willHideObservable = NotificationCenter.default.rx.notification(UIResponder.keyboardWillHideNotification)
            .map { noti -> CGFloat in 0 }

2. Notification 옵저버블 구독하는 옵저버 추가

       Observable.merge(willShowObservable, willHideObservable)
            .map { [unowned self] height -> UIEdgeInsets in
                var inset = self.textView.contentInset
                inset.bottom = height
                return inset
            }
            .subscribe(onNext: { [weak self] inset in
                UIView.animate(withDuration: 0.3) {
                    self?.textView.contentInset = inset
                }
            })
            .disposed(by: bag)
profile
iOS 개발자가 되기 위한 스터디룸/스터디의 레퍼런스는 모두 kxcoding

0개의 댓글