lunes, 22 de febrero de 2016

iOS: Segue

Add a Segue to navigate forward
Scenes are connected by segues, which represent a transition between two view controllers: the source and the destination. 
Before creating a segue, you need to configure your scenes. First, you’ll wrap your view controller in a navigation controller. 

To add a navigation controller to your table view controller

  1. In the outline view, select View Controller.
  2. With the view controller selected, choose Editor > Embed In > Navigation Controller.


(Note: You can hide the Top Bar: Click on top of your view controller in the Document Outline then Utilities > Attributes inspector > Top Bar (None))

Use storyboards to define navigation
There are several types of segues you can create in a storyboard:
  • Show. A show segue pushes new content on top of the current view controller stack. Where the content shows up depends on the layout of view controllers in the scene.
  • Show detail. A show detail segue either pushes new content on top of the current view controller stack or replaces the content that’s shown, depending on the layout of view controllers in the scene.
  • Present modally. A modal segue is one view controller presenting another controller modally, requiring a user to perform an operation on the presented controller before returning to the main flow of the app. A modal view controller isn’t added to a navigation stack; instead, it’s generally considered to be a child of the presenting view controller. The presenting view controller is responsible for dismissing the modal view controller it created and presented.
  • Popover presentation. The presented view controller is shown as a popover anchored to an existing view.
  • Custom. You can define your own custom transition by subclassing UIStoryboardSegue.
  • Unwind. An unwind segue moves backward through one or more segues to return the user to an existing instance of a view controller. You use unwind segues to implement reverse navigation.
In addition to segues, scenes may be connected by a relationship. For example, there’s a relationship between the navigation controller and its root view controller. In this case, the relationship represents the containment of the root view controller by the navigation controller. 

Unwind segues

Create unwind segue:

Supposed you have the following A > B > C. To navigate from C back to A:

1) Create an unwind segue in A. E.g.:

- (IBAction)dismiss:(UIStoryboardSegue*)segue
{
    

}

2) Create an unwind segue in C whose action is "dismiss:" from A:

viernes, 19 de febrero de 2016

iOS: Moving content that is located under the Keyboard

Swift

override func viewDidLoad() {
     
        self.registerForKeyboardNotifications()
    }
 
    private func registerForKeyboardNotifications() {
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: .UIKeyboardDidShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: .UIKeyboardWillHide, object: nil)
    }
 
    @objc func keyboardWasShown(notification: Notification) {
        
        if let info = notification.userInfo,
            let kbSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue,
            let activeTextField = self.activeTextField {
            
            let inputAccesoryViewHeight = activeTextField.inputAccessoryView?.frame.height ?? 0
            
            if (keyboardHeight ?? 0) < kbSize.height {
                keyboardHeight = kbSize.height
            }
            
            let finalHeight = keyboardHeight! + inputAccesoryViewHeight
            
            let contentInsets = UIEdgeInsetsMake(0, 0, finalHeight, 0)
            self.scrollView.contentInset = contentInsets
            self.scrollView.scrollIndicatorInsets = contentInsets
            
            var aRect = self.view.frame
            aRect.size.height -= keyboardHeight!
            
            let frame = self.scrollView.convert(activeTextField.frame, from: activeTextField.superview)
            
            var visibleRect = CGRect()
            visibleRect.origin = self.scrollView.contentOffset
            visibleRect.size = self.scrollView.bounds.size
            
            if (!aRect.contains(frame.origin)) {
                self.scrollView.scrollRectToVisible(frame, animated: true)
            }
        }
    }
 
    @objc func keyboardWillBeHidden(notification: Notification) {
     
        let contentInsets = UIEdgeInsets.zero
        self.scrollView.contentInset = contentInsets
        self.scrollView.scrollIndicatorInsets = contentInsets
    }
 
    // UITextFieldDelegate
 
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        self.activeTextField = textField
     
        return true
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField == self.activeTextField {
            self.activeTextField = nil
        }
    }