아삭아삭 iOS 개발

[TIL] 2O22.07.07 (SeSAC iOS) 본문

TIL(Today I Learned)

[TIL] 2O22.07.07 (SeSAC iOS)

바닐라머스크 2022. 7. 7. 23:27

 

오늘 공부한 내용들을 내가 잊지 않기 위해 기록합니다.

틀린 내용이 있을 수 있는 점 참고 부탁드립니다 :)

 

■ 오늘의 과제

1) LEDBoard Project

  • [TextField ReturnKey]
    - TextField에서 입력하다가 엔터키 누를 경우 키보드 내려가게 설정하기
    - TextField 액션 연결할 때 이벤트를 DidEndOnExit으로 설정해서 동작하는 구조 확인해보기
    func designTextField() {
    // 1. 텍스트필드의 returnketype 설정
    userTextField.returnKeyType = .done
    }
        
    // 2.TextField에서 엔터키 누르면 키보드 내려가는 액션 설정
    @IBAction func didEndOnExit(_ sender: UITextField) {
        print("엔터키 누르면 키보드가 내려갑니다.")
    }
타이핑 후 엔터키(done)을 누르면 키보드가 쓩 내려간다
  • [View isHidden]
    - TapGesture에 액션으로 추가해보기
    - TapGesture를 클릭할 경우, 상단의 view를 toggle형태로 숨겨졌다가 다시 보여지는 기능 적용해보기
       (view의 숨김여부를 판단해서 숨겨져 있으면 보이고, 보이면 숨기도록)
    @IBAction func tapGestureClicked(_ sender: UITapGestureRecognizer) {
        
        // view의 숨김여부에 따른 view 표기여부 toggle 로직
        if (searchView.isHidden == true) {
            searchView.isHidden = false
        } else {
            searchView.isHidden = true
        }
    }
전체 view를 tap 할 때마다 상단의 흰색 searchview의 표기여부가 toggle되어 반응함


 2) Netflix Project

  • 함수와 매개변수, 옵셔널의 개념을 활용해서 어제 작업했던 과제의 코드를 보다 명확하게 수정해보기

<AS-IS>

이전에는 하나하나 항목별로 설정을 한줄씩 직접 입력했고, 그 사항들을 함수 하나로 묶어서 setUI()를 viewDidLoad안에 넣어서 호출시점을 정해주었었다.

이해하기에는 쉬웠지만 좀더 효율적인 방법이 필요했다.

func setUI() {
	// 모서리 둥글게 만들기
	emailTextField.layer.cornerRadius = 8
	passwordTextField.layer.cornerRadius = 8
	nickNameTextFIeld.layer.cornerRadius = 8
	locationTextField.layer.cornerRadius = 8
	recCodeTextField.layer.cornerRadius = 8

	// textfield 배경색상 설정
	emailTextField.backgroundColor = UIColor.systemGray
	passwordTextField.backgroundColor = UIColor.systemGray
	nickNameTextFIeld.backgroundColor = UIColor.systemGray
	locationTextField.backgroundColor = UIColor.systemGray
	recCodeTextField.backgroundColor = UIColor.systemGray
	
	// textfield내 폰트설정
	emailTextField.font = .systemFont(ofSize: 15)
	passwordTextField.font = .systemFont(ofSize: 15)
	nickNameTextFIeld.font = .systemFont(ofSize: 15)
	locationTextField.font = .systemFont(ofSize: 15)
	recCodeTextField.font = .systemFont(ofSize: 15)

	// textfield네 text 정렬
	emailTextField.textAlignment = .center
	passwordTextField.textAlignment = .center
	nickNameTextFIeld.textAlignment = .center
	locationTextField.textAlignment = .center
	recCodeTextField.textAlignment = .center
	
	// 키보드 설정
	emailTextField.keyboardType = .emailAddress
	passwordTextField.keyboardType = .asciiCapable
	nickNameTextFIeld.keyboardType = .default
	locationTextField.keyboardType = .default
	recCodeTextField.keyboardType = .numberPad
	
	// placeholder 설정
	emailTextField.attributedPlaceholder = NSAttributedString(string: "이메일 주소 또는 전화번호", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])
	passwordTextField.attributedPlaceholder = NSAttributedString(string: "비밀번호", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])
	nickNameTextFIeld.attributedPlaceholder = NSAttributedString(string: "닉네임", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])
	locationTextField.attributedPlaceholder = NSAttributedString(string: "위치", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])
	recCodeTextField.attributedPlaceholder = NSAttributedString(string: "추천 코드 입력", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])
}

비슷한.. 내용을 반..복..

 

<TO-BE>

위에서 정리한 내용들에서 반복적으로 설정해주는 요소들을 묶어서 함수로 정의해줬다.

그리고 그 함수의 매개변수와 전달인자로 textfield는 누구인지, 각각의 배경색상(UIColor), 키보드 종류, placeholder(String) 는 무엇인지 등을 설정해주었다.

깔꼼하게 정리가 싹 된 코드를 보니 기분이 좋다! :)

이렇게 함수를 이용하니 TextField 설정 관련한 코드는 42줄에서 20줄로 거의 1/2이 되었다! 워후!

효율적인 방법인 만큼 앞으로 잘 활용해보아야 겠다.

override func viewDidLoad() {
    super.viewDidLoad()
    designTextField(emailTextField, keyboardType: .emailAddress, placeholder: "이메일 주소 또는 전화번호", isSecureTextEntry: false)
    designTextField(passwordTextField, keyboardType: .asciiCapable, placeholder: "비밀번호", isSecureTextEntry: true)
    designTextField(nickNameTextFIeld, keyboardType: .default, placeholder: "닉네임", isSecureTextEntry: false)
    designTextField(locationTextField, keyboardType: .default, placeholder: "위치", isSecureTextEntry: false)
    designTextField(recCodeTextField, keyboardType: .numberPad, placeholder: "추천 코드 입력", isSecureTextEntry: false)
}
    
func designTextField(_ textFieldName: UITextField, keyboardType: UIKeyboardType, placeholder: String, isSecureTextEntry: Bool) {
    textFieldName.layer.cornerRadius = 8
    textFieldName.backgroundColor = UIColor.systemGray
    textFieldName.font = .systemFont(ofSize: 15)
    textFieldName.textAlignment = .center
    textFieldName.keyboardType = keyboardType
    textFieldName.attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])
    textFieldName.autocorrectionType = .no
    if #available(iOS 12.0, *) { textFieldName.textContentType = .oneTimeCode }
    textFieldName.isSecureTextEntry = isSecureTextEntry
}

함수의 매개변수와 전달인자를 활용해서 정리싹~!

 

어제 날려버린 넷플릭스 프로젝트도 오늘 후다닥 다시 만들어서 수습했고,

심지어 오늘 배운 내용 복습하면서 코드도 깔끔하게 정리할 수 있어서 뿌듯하다.

 

ios 개발공부랑 과제하는거 너무 재밌다~~~!!!

 

 

 

참고 자료

[1] https://ios-development.tistory.com/475

[2] https://www.youtube.com/watch?v=jxkN8xKYmwM 

[3] SeSAC 2기 7월 7일 오늘 배운 내용

 

 

'TIL(Today I Learned)' 카테고리의 다른 글

[TIL] 2022.07.09~10 (SeSAC iOS)  (0) 2022.07.11
[TIL] 2022.07.08 (SeSAC iOS)  (0) 2022.07.08
[TIL] 2O22.07.06 (SeSAC iOS)  (2) 2022.07.06
[TIL] 2022.07.05 (SeSAC iOS)  (0) 2022.07.05
[TIL] 2022.07.04 (SeSAC iOS)  (0) 2022.07.04