https://github.com/hasen-sprung/iOS_Canvas/commit/ed2c0335d91dc577a42f9866902c77c87f631f6a
trim.7A9243CF-6189-4D2D-94FD-0C70F976277C.MOV
ㅎㅎ 완성
제약사항을 클래스 프로퍼티로 저장 (self.bottomConstraint)
CRBackgroundView.snp.makeConstraints { make in
make.top.equalTo(self.snp.top).offset(self.frame.width / 7 * 2)
make.trailing.equalTo(self.snp.trailing).offset(-40)
make.leading.equalTo(self.snp.leading).offset(40)
// TODO: KEYBOARD NOTIFICATION
self.bottomConstraint = make.bottom.equalTo(self.snp.bottom).offset(-40).constraint
self.addSubview(CRBackgroundView)
CRBackgroundView.backgroundColor = .blue
}
노티 등록 후, 키보드 정보를 가져오고, 제약 사항을 업데이트하고, 레이아웃 적용
override init(frame: CGRect) {
super.init(frame: frame)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
...
@objc
func keyboardWillShow(_ sender: Notification) {
print("show keyboard")
if let userInfo = sender.userInfo as? Dictionary<String, Any> {
if let keyboardFrameValue = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardFrame = keyboardFrameValue.cgRectValue
let containerViewOriginPlusHeight = CRBackgroundView.frame.origin.y + CRBackgroundView.frame.size.height
self.bottomConstraint?.update(offset: -keyboardFrame.height)
self.setNeedsLayout()
UIView.animate(withDuration: 0.4, animations: {
self.layoutIfNeeded()
})
}
}
}
@objc
func keyboardWillHide(_ sender: Notification) {
print("hide keyboard")
self.bottomConstraint?.update(offset:-40)
self.setNeedsLayout()
UIView.animate(withDuration: 0.4, animations: {
self.layoutIfNeeded()
})
}
self.layoutIfNeeded()
//강제로 레이아웃을 즉시 적용시키는데 안전한지는 모르겠다.
프레임 기반 레이어나 그림자 효과를 세팅
https://dzone.com/articles/calayer-and-auto-layout-with-swift-1