-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
// 텍스트 필드에 input accessory view 추가하는 방법 티스토리 포스트
텍스트 필드 선언 코드
NetworkSocketProgrammingWithSwiftBsd/SoTalk/Source/Feature/Chat/View/GroupChatSearch.swift
Lines 31 to 45 in 8cf0f90
private let textField = UITextField().set { | |
$0.translatesAutoresizingMaskIntoConstraints = false | |
$0.font = .systemFont(ofSize: Constant.TextField.size) | |
$0.textColor = Constant.TextField.textColor | |
let comment = "그룹 이름을 검색하세요." | |
let placeholderAttrStr = NSMutableAttributedString( | |
string: comment) | |
let attributes: [NSAttributedString.Key: Any] = [ | |
.foregroundColor: Constant.TextField.placeholderColor, | |
.font: UIFont.systemFont(ofSize: Constant.TextField.size)] | |
placeholderAttrStr.addAttributes( | |
attributes, | |
range: NSRange(location: 0, length: comment.count)) | |
$0.attributedPlaceholder = placeholderAttrStr | |
} |
텍스트 필드 input accessory view의 버튼 선언 코드
텍스트 필드 input accessory view용 인스턴스로 컨테이너 뷰 생성 + 버튼 auto layout으로 등록 + UITextFieldDelegate
NetworkSocketProgrammingWithSwiftBsd/SoTalk/Source/Feature/Chat/View/GroupChatSearch.swift
Lines 79 to 116 in 8cf0f90
func initInputAccessoryView() { | |
// constant | |
let height: CGFloat = 75.0 | |
let origin: CGPoint = .init(x: 0, y: -300) | |
let size: CGSize = .init( | |
width: UIScreen.main.bounds.width, | |
height: height) | |
let containerView = UIView( | |
frame: CGRect(origin: origin, size: size)) | |
textField.inputAccessoryView = containerView | |
} | |
var accessoryView: UIView? { | |
textField.inputAccessoryView | |
} | |
func setInputAccessoryViewWithButton() { | |
searchButton.setInputAccessoryViewLayout( | |
from: accessoryView, | |
spacing: .init(leading: 24, trailing: 24)) | |
} | |
func hideKeyboard() { | |
textField.resignFirstResponder() | |
} | |
} | |
extension GroupChatSearch: UITextFieldDelegate { | |
func textFieldShouldReturn(_ textField: UITextField) -> Bool { | |
textField.resignFirstResponder() | |
} | |
func textFieldDidEndEditing(_ textField: UITextField) { | |
textField.resignFirstResponder() | |
} | |
} |
텍스트 필드 이외의 화면 클릭시 텍스트 필드 first responder 해제
Lines 57 to 64 in 8cf0f90
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
super.touchesBegan(touches, with: event) | |
guard let touch = touches.first else { return } | |
let touchLocation = touch.location(in: self.view) | |
if !naviBottomView.searchBarView.frame.contains(touchLocation) { | |
naviBottomView.hideKeyboard() | |
} | |
} |
Metadata
Metadata
Assignees
Labels
No labels