Xcode Collection of Useful Functions and Tips
Tags: ios, tutorial, swift, xcode
Published: 2021-04-22 14:48:49

## Table of Contents
* [Scroll View](#scroll-view)
* [Keyboard Dismiss](#keyboard-dismiss)
* [Backpropagation Function](#backpropagation-function)
* [Loading Icon](#loading-icon)
* [Light/Dark Mode](#light/dark-mode)

## Extras
When writing a tutorial for my latest iOS app, [SnapShot](https://blog.sashido.io/snapshot-how-to-create-a-digital-scrapbook-in-ios-part-1/), there were bits of code that appeared often. I thought these steps were generally useful, so I created this collection of them to post separately. 

### Scroll View
Below are my steps to incorporating a scroll view, but for more visual instruction check out [this video](https://www.youtube.com/watch?v=nfHBCQ3c4Mg).
&nbsp;&nbsp;&nbsp;&nbsp; 1\. Add a scroll view to the chosen controller
&nbsp;&nbsp;&nbsp;&nbsp; 2\. Drag it so it fills up the whole screen
&nbsp;&nbsp;&nbsp;&nbsp; 3\. Hover over the scrollView -> click control -> drag to View -> select Leading/Top/Trailing/Bottom Space to Safe Area
&nbsp;&nbsp;&nbsp;&nbsp; 4\. Add a view on top of the scrollView
&nbsp;&nbsp;&nbsp;&nbsp; 5\. Repeat steps 2 & 3 but with the view and scroll view, also clicking <u>Equal Widths/Heights</u>
&nbsp;&nbsp;&nbsp;&nbsp; 6\. Click on the equal heights constraints and in the <u>Attributes Inspector</u>, set the priority to 250
&nbsp;&nbsp;&nbsp;&nbsp; 7\. Then click the controller -> Size Inspector -> Simulated Size -> Freeform and set the height to a suitable value (for signup: 950)
&nbsp;&nbsp;&nbsp;&nbsp; 8\. Now add all the elements and make sure to add constraints to everything (otherwise the scrollView won’t work)
&nbsp;&nbsp;&nbsp;&nbsp; 9\. Finally, click scrollView -> Attributes Inspector -> Scroll View -> Keyboard -> Dismiss Interactively

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ex82wwh1r6i88umwreas.png)

### Keyboard Dismiss
You can override touchesBegan to dismiss the keyboard:
```swift
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        // option 1
        self.view.endEditing(true)
        // option2
        usernameTextField.resignFirstResponder()
        passwordTextField.resignFirstResponder()
}
```

### Backpropagation Function
&nbsp;&nbsp;&nbsp;&nbsp; 1\. In **viewDidLoad** of the controller, you want the function to run in:
```swift
NotificationCenter.default.addObserver(self, selector: #selector(reloadContent), name: Notification.Name("reloadContent"), object: nil)
```
&nbsp;&nbsp;&nbsp;&nbsp; 2\. In the same file, add the function:
```swift
@objc func reloadContent (notification: NSNotification){
        // stuff you want to run in func - e.g...
        self.memTitle.text = page.title
        memTitle.adjustsFontSizeToFitWidth = true
        self.memImage.image = page.Image
        self.memDesc.text = page.desc
        self.memCategory.text = page.category
        self.memLocation.text = page.location
        self.memTags.text = page.tags
        self.memDate.text = page.date
    }
```
&nbsp;&nbsp;&nbsp;&nbsp; 3\. In the controller you want to call the function from, write:
```swift
NotificationCenter.default.post(name: Notification.Name("reloadContent"), object: nil)
```

### Loading Icon
&nbsp;&nbsp;&nbsp;&nbsp; 1\. In the <u>Classes</u> folder, create a new file called **AIUtil.swift**
&nbsp;&nbsp;&nbsp;&nbsp; 2\. Add the following code:
```swift
@objc func reloadContent (notification: NSNotification) {
        self.memTitle.text = page.title
        memTitle.adjustsFontSizeToFitWidth = true
        self.memImage.image = page.Image
        self.memDesc.text = page.desc
        self.memCategory.text = page.category
        self.memLocation.text = page.location
        self.memTags.text = page.tags
        self.memDate.text = page.date
    }
```
&nbsp;&nbsp;&nbsp;&nbsp; 3\. When you want to show the spinner: *self.showSpinner()*
&nbsp;&nbsp;&nbsp;&nbsp; 4\. When you want to remove the spinner: *self.removeSpinner()*

### Light/Dark mode
If your computer’s simulation shows the default color as white (lightmode) but your phone is in darkmode, the colors will be black when you run the app on your phone. This is an issue I ran into, but luckily there is an easy solution:
&nbsp;&nbsp;&nbsp;&nbsp; 1\. Go to **SceneDelegate.swift**
&nbsp;&nbsp;&nbsp;&nbsp; 2\. In the first function (scene), add the following code:
```swift
if #available(iOS 13.0, *) {
        window?.overrideUserInterfaceStyle = .light // for light mode
        window?.overrideUserInterfaceStyle = .dark // for dark mode
}
```

## Resources
SnapShot Part 1: https://blog.sashido.io/snapshot-how-to-create-a-digital-scrapbook-in-ios-part-1/
SnapShot Part 2: https://blog.sashido.io/snapshot-how-to-create-a-digital-scrapbook-in-ios-part-2/
SashiDo: https://www.sashido.io/en/
Parse Documentation: https://docs.parseplatform.org/ios/guide/
Parse Video Playlist: https://www.youtube.com/playlist?list=PLMRqhzcHGw1ZFjFyHGJTTPuvcLbwVCuG4
GitHub: https://github.com/CarolineBaillie/snapShot