जब कीबोर्ड को दिखाया और मेरे आवेदन से छिपाया गया है तो मैं कैसे पता लगा सकता हूं?
जब कीबोर्ड को दिखाया और मेरे आवेदन से छिपाया गया है तो मैं कैसे पता लगा सकता हूं?
जवाबों:
कीबोर्ड के बारे में संदेश सुनने के लिए अपनी कक्षा की ViewDidLoad विधि में:
// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
फिर आपके द्वारा निर्दिष्ट तरीकों में (इस मामले में ) keyboardDidShowऔर keyboardDidHideआप इसके बारे में कुछ कर सकते हैं:
- (void)keyboardDidShow: (NSNotification *) notif{
// Do something here
}
- (void)keyboardDidHide: (NSNotification *) notif{
// Do something here
}
UITextFieldDelegatई बनाएं , फिर textFieldShouldReturn:विधि लागू करें । आपको textFieldबस एक तर्क के रूप में दर्ज किया जाएगा , जिसे आप अपने स्वयं के TextFields से तुलना कर सकते हैं और स्क्रॉल कर सकते हैं scrollViewताकि उपयुक्त TextField दिखाई दे रहा है।
आपको बस जरूरत पड़ सकती addObserverहै viewDidLoad। लेकिन addObserverमें viewWillAppearऔर दुर्लभ दुर्घटनाओं removeObserverको viewWillDisappearरोकता है जो तब होता है जब आप अपना दृष्टिकोण बदल रहे होते हैं।
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: UIResponder.keyboardWillShowNotification, object: nil)
}
@objc func keyboardWillAppear() {
//Do something here
}
@objc func keyboardWillDisappear() {
//Do something here
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: Notification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: Notification.Name.UIKeyboardWillShow, object: nil)
}
@objc func keyboardWillAppear() {
//Do something here
}
@objc func keyboardWillDisappear() {
//Do something here
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillAppear:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillDisappear:", name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillAppear(notification: NSNotification){
// Do something here
}
func keyboardWillDisappear(notification: NSNotification){
// Do something here
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self)
}
deinitइस तरह से निकालना बेहतर है :deinit { NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil) }
deinit { NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) }
स्विफ्ट 3:
NotificationCenter.default.addObserver(self, selector: #selector(viewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(viewController.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
func keyboardWillShow(_ notification: NSNotification){
// Do something here
}
func keyboardWillHide(_ notification: NSNotification){
// Do something here
}
स्विफ्ट 4:
NotificationCenter.default.addObserver( self, selector: #selector(ControllerClassName.keyboardWillShow(_:)),
name: Notification.Name.UIKeyboardWillShow,
object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ControllerClassName.keyboardWillHide(_:)),
name: Notification.Name.UIKeyboardWillHide,
object: nil)
अगला, ऑब्जेक्ट का जीवन समाप्त होने पर सूचनाओं को सुनने से रोकने के लिए विधि जोड़ना: -
Then add the promised methods from above to the view controller:
deinit {
NotificationCenter.default.removeObserver(self)
}
func adjustKeyboardShow(_ open: Bool, notification: Notification) {
let userInfo = notification.userInfo ?? [:]
let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let height = (keyboardFrame.height + 20) * (open ? 1 : -1)
scrollView.contentInset.bottom += height
scrollView.scrollIndicatorInsets.bottom += height
}
@objc func keyboardWillShow(_ notification: Notification) {
adjustKeyboardShow(true, notification: notification)
}
@objc func keyboardWillHide(_ notification: Notification) {
adjustKeyboardShow(false, notification: notification)
}
+=सन्निवेश वाली अधिकाधिक प्राप्त करने के लिए प्रकट होता है।
UIResponder.keyboardWillShowNotificationऔर UIResponder.keyboardWillHideNotification, और कीबोर्ड जानकारी कुंजी है UIResponder.keyboardFrameBeginUserInfoKey।
स्विफ्ट - 4
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
addKeyBoardListener()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self) //remove observer
}
func addKeyBoardListener() {
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil);
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil);
}
@objc func keyboardWillShow(_ notification: Notification) {
}
@objc func keyboardWillHide(_ notification: Notification) {
}
की जाँच करें कीबोर्ड प्रबंध की धारा "पाठ, वेब, और गाइड प्रोग्रामिंग संपादन" कुंजीपटल ट्रैक करने संबंधी जानकारी से पता चला या छिपा जा रहा है, और कैसे प्रदर्शित / इसे मैन्युअल रूप से खारिज करने के लिए।
आप 2 कीबोर्ड सूचनाओं के लिए खुद को पंजीकृत करना चाहेंगे:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];
अपने TextField को कीबोर्ड पर समायोजित करने के तरीके पर महान पोस्ट - http://iosdevelopertips.com/user-interface/adjust-textfield-hidden-by-keyboard.html
स्विफ्ट 4.2 में अधिसूचना नाम एक अलग नामस्थान में चले गए हैं। तो अब यह है
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
addKeyboardListeners()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
func addKeyboardListeners() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
}
@objc private extension WhateverTheClassNameIs {
func keyboardWillShow(_ notification: Notification) {
// Do something here.
}
func keyboardWillHide(_ notification: Notification) {
// Do something here.
}
}
ऊपर दिए गए उत्तर सही हैं। हालांकि मैं इसे लपेटने के लिए एक सहायक बनाना पसंद करूंगा notification's observers।
extension KeyboardHelper {
enum Animation {
case keyboardWillShow
case keyboardWillHide
}
typealias HandleBlock = (_ animation: Animation, _ keyboardFrame: CGRect, _ duration: TimeInterval) -> Void
}
final class KeyboardHelper {
private let handleBlock: HandleBlock
init(handleBlock: @escaping HandleBlock) {
self.handleBlock = handleBlock
setupNotification()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
private func setupNotification() {
_ = NotificationCenter.default
.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main) { [weak self] notification in
self?.handle(animation: .keyboardWillShow, notification: notification)
}
_ = NotificationCenter.default
.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { [weak self] notification in
self?.handle(animation: .keyboardWillHide, notification: notification)
}
}
private func handle(animation: Animation, notification: Notification) {
guard let userInfo = notification.userInfo,
let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double
else { return }
handleBlock(animation, keyboardFrame, duration)
}
}
private var keyboardHelper: KeyboardHelper?
...
override func viewDidLoad() {
...
keyboardHelper = KeyboardHelper { [unowned self] animation, keyboardFrame, duration in
switch animation {
case .keyboardWillShow:
print("keyboard will show")
case .keyboardWillHide:
print("keyboard will hide")
}
}
}
स्विफ्ट 4 -dd 20 october 2017
override func viewDidLoad() {
[..]
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear(_:)), name: Notification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(_:)), name: Notification.Name.UIKeyboardWillShow, object: nil)
}
@objc func keyboardWillAppear(_ notification: NSNotification) {
if let userInfo = notification.userInfo,
let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue).cgRectValue {
let inset = keyboardFrame.height // if scrollView is not aligned to bottom of screen, subtract offset
scrollView.contentInset.bottom = inset
scrollView.scrollIndicatorInsets.bottom = inset
}
}
@objc func keyboardWillDisappear(_ notification: NSNotification) {
scrollView.contentInset.bottom = 0
scrollView.scrollIndicatorInsets.bottom = 0
}
deinit {
NotificationCenter.default.removeObserver(self)
}
यदि आपके पास अधिक है, तो एक UITextFieldएस और आपको कुछ करने की आवश्यकता है जब (या इससे पहले) कीबोर्ड प्रकट होता है या गायब हो जाता है, तो आप इस दृष्टिकोण को लागू कर सकते हैं।
UITextFieldDelegateअपनी कक्षा में जोड़ें । पूर्णांक काउंटर असाइन करें, मान लें:
NSInteger editCounter;
इस काउंटर को शून्य में सेट करें viewDidLoad। फिर, तरीकों को लागू करने textFieldShouldBeginEditingऔर textFieldShouldEndEditingप्रतिनिधि।
पहले एक में editCounter के लिए 1 जोड़ें। अगर EditCounter का मान 1 हो जाता है - इसका मतलब है कि कीबोर्ड दिखाई देगा (यदि आप YES लौटाते हैं तो)। यदि editCounter> 1 - इसका मतलब है कि कीबोर्ड पहले से ही दिखाई दे रहा है और एक और UITextField फोकस रखता है।
में textFieldShouldEndEditingeditCounter से घटाना 1। यदि आपको शून्य मिलता है - कीबोर्ड खारिज कर दिया जाएगा, अन्यथा यह स्क्रीन पर रहेगा।
आप KBKeyboardObserver लाइब्रेरी का उपयोग कर सकते हैं । इसमें कुछ उदाहरण हैं और सरल इंटरफ़ेस प्रदान करता है।
NSNotificationCentrयहाँ कीबोर्ड की दृश्यता के लिए अवलोकन की सुविधा के लिए एक कोकोआपोड्स है: https://github.com/levantAJ.KKhi
pod 'Keyhi'
तो आह, यह अब असली जवाब है।
import Combine
class MrEnvironmentObject {
/// Bind into yr SwiftUI views
@Published public var isKeyboardShowing: Bool = false
/// Keep 'em from deallocatin'
var subscribers: [AnyCancellable]? = nil
/// Adds certain Combine subscribers that will handle updating the
/// `isKeyboardShowing` property
///
/// - Parameter host: the UIHostingController of your views.
func setupSubscribers<V: View>(
host: inout UIHostingController<V>
) {
subscribers = [
NotificationCenter
.default
.publisher(for: UIResponder.keyboardWillShowNotification)
.sink { [weak self] _ in
self?.isKeyboardShowing = true
},
NotificationCenter
.default
.publisher(for: UIResponder.keyboardWillHideNotification)
.sink { [weak self, weak host] _ in
self?.isKeyboardShowing = false
// Hidden gem, ask me how I know:
UIAccessibility.post(
notification: .layoutChanged,
argument: host
)
},
// ...
Profit
.sink { [weak self] profit in profit() },
]
}
}