Showing posts with label NSNotificationCenter. Show all posts
Showing posts with label NSNotificationCenter. Show all posts

Monday, June 6, 2016

Changing View When Showing Keyboard

 
var keyboardIsShowing = false

override func viewDidAppear(_ animated: Bool) {  
    NotificationCenter.default.addObserver(self, selector: 
        #selector(keyboardDidShow), 
        name: Notification.Name.UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: 
        #selector(keyboardWillHide), 
        name: Notification.Name.UIKeyboardWillHide, object: nil)
}

override func viewWillDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self, 
        name: Notification.Name.UIKeyboardDidShow, object: self)
    NotificationCenter.default.removeObserver(self, 
        name: Notification.Name.UIKeyboardWillHide, object: self)
}
 

Not In A Scroll View

 
@objc 
func keyboardDidShow(notification: NSNotification) {
    
    if keyboardIsShowing { return }
    
    let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
        
    keyBoardIsShowing = true
        
    var newFrame = self.view.frame
    newFrame.origin.y -= keyboardSize.height
    self.view.frame = newFrame
}

@objc 
func keyboardWillHide(notification: NSNotification) {
    let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
        
    keyboardIsShowing = false
        
    var newFrame = self.view.frame
    newFrame.origin.y += keyboardSize.height
}
 
Note: While this works fine, I always had trouble getting it to show smoothly or even animate it. It is choppy. As of this date I recommend just putting your view in a scroll view and using the below methods for a smoother look and feel.

In A Scroll View

 
@IBOutlet weak var scrollView: UIScrollView!

@objc 
func keyboardDidShow(notification: NSNotification) {
    
    if keyboardIsShowing { return }
    
    let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
        
    keyboardIsShowing = true
    
    let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets

    // OPTIONAL: When you want bottom of a view to rest on the top of the keyboard.
    let viewBottom = myView.frame.origin.y + myView.frame.height
    let keyboardTop = view.bounds.height - keyboardSize.height

    if viewBottom > keyboardTop {
        let y = viewBottom - keyboardTop + 5
        scrollView.setContentOffset(CGPoint(x: 0, y: y), animated: true)
    }
}

@objc 
func keyboardWillHide(notification: NSNotification) {
    keyboardIsShowing = false

    let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}
 

(Xcode 9, Swift 4)

Saturday, May 7, 2016

Notifications

Using NotificationCenter you can communicate from one class to another. For example, while executing code in one class you can notify the UI to update something.

Basic Setup

In a separate class you might "post" a notification. In this example I named the post "updateView". Notifications is a 3 step process:

Step 1 - Setup Post Names

 
extension Notification.Name {
    static let updateView = NSNotification.Name("updateView")
}
 

Step 2 - Post Notification

To pass data, you can send a dictionary to the userInfo parameter.
 
var userInfo: [String: String] = ["extraData": "Some extra data"]
userInfo["moreData"] = "Some more data"
NotificationCenter.default.post(name: .updateView, object: self, 
    userInfo: userInfo)
 

Step 3 - Observe Notification

Then in your ViewController you might be "observing" for that particular post called "updateView" in the notification center:
 
override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver
            (forName: .updateView, object: nil, 
             queue: OperationQueue.main) {
        (notification) in
        // Get values from the class that sent the notification
        let poster = notification.object as! ClassThatPostedNotification
        let propertyValue = poster.property

        // Get values from userInfo
        let extraData = notification.userInfo?["extraData"] as! String
        let moreData = notification.userInfo?["moreData"] as! String
    }
}
 

So the ViewController will be observing any post called "updateView" and when it observes a post it calls updateView. I usually name my posts and functions the same name because...well, I am a simple guy. :)

(Swift 4)

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...