Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] SoTalk 메인 화면 텍스트 필드 클릭시 input accessoryview 등록 #1

Open
SHcommit opened this issue May 29, 2023 · 0 comments

Comments

@SHcommit
Copy link
Owner

SHcommit commented May 29, 2023

// 텍스트 필드에 input accessory view 추가하는 방법 티스토리 포스트

화면

텍스트 필드 선언 코드

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의 버튼 선언 코드

https://github.com/SHcommit/NetworkSocketProgrammingWithSwiftBsd/blob/8cf0f90a8ea14b7318c1cd50427130dcc5eddcb2/SoTalk/Source/Feature/Chat/View/GroupChatSearch.swift#LL53C1-L53C1

텍스트 필드 input accessory view용 인스턴스로 컨테이너 뷰 생성 + 버튼 auto layout으로 등록 + UITextFieldDelegate

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 해제

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()
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant