당신이 설명하는 동작은 UI 요소의 경우 부울 isEnabled
값을 조작하여 수행 할 수있다. 이것을 매끄럽고 사용자 친화적으로 만드는 몇 가지 단계가 있습니다.
그냥 좋은 방법은,이 예를 들어 필요하지 않습니다,하지만 난의 MyViewController의 확장을 구독하는/키보드 알림 취소 좋아하고 그래서 거기에, 그래서 같은 :
import Foundation
import UIKit
extension MyViewController {
//MARK: Subscribing/Unsubribing to NotificationCenter.
func subcribeToKeyboardNotifications(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func unsubscribeToKeyboardNotifications(){
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
//MARK: Screen manipulation methods, to move the UIView up when the bottom text field is editing.
func keyboardWillShow(_ notification: Notification) {
if bottomTextField.isEditing {
view.frame.origin.y = getKeyboardHeight(notification) * -1
}
}
func keyboardWillHide(_ notification: Notification) {
if bottomTextField.isEditing {
view.frame.origin.y = 0
}
}
//MARK: Value returning method to calculate keyboard height.
private func getKeyboardHeight(_ notification: Notification) -> CGFloat {
let userInfo = (notification as NSNotification).userInfo!
let keyboardSize = userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue
return keyboardSize.cgRectValue.height
}
}
다음, MyViewController에서 리턴 키를 누를 때 키보드를 resignToFirstResponder()
으로 설정하고 내 UITextFieldDelegates
을 설정했는지 확인합니다.
import Foundation
import UIKit
class MyViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var someButton: UIButton!
@IBOutlet weak var topTextField: UITextField!
@IBOutlet weak var bottomTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//Set the delegates to self, it this example.
topTextField.delegate = self
bottomTextField.delegate = self
enableUIElements(true)
}
//Delegate function to type stuff into the text field.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
enableUIElements(false)
var newText = textField.text! as NSString
newText = newText.replacingCharacters(in: range, with: string) as NSString
let textSize: CGSize = newText.size(attributes: [NSFontAttributeName: textField.font!])
return textSize.width < textField.bounds.size.width
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder() //Dismiss the keyboard.
enableUIElements(true) //enable the UI elements.
return true
}
//Create a function to enable/disable UI elements.
func enableUIElements(_ enable: Bool){
someButton.isEnabled = enable
//TODO: add other elements here.
}
}
마지막 기능 !!! 키보드를 활성화 한 상태에서 다른 요소를 활성화/비활성화하려면이 방법을 사용하십시오. 당신이 UITextField에 인스턴스에 초점을 맞추고있는 동안보기에
자동으로 실행하면 안됩니다. 프로그래밍을 통해 다른 모든 기능을 사용하지 않도록 프로그래밍해야합니다. –
오른쪽. 그게 어떻게 해야할지 궁금해하는거야. userInteractionEnabled가 모든 상호 작용을 중지 할 수 있지만 여전히 사용자가 키보드와 상호 작용할 수 있기를 원합니다. – brownmamba
예를 들어 UI 버튼의 경우 yourButtonName을 수행해야합니다.isEnabled = false –