ios_topics icon indicating copy to clipboard operation
ios_topics copied to clipboard

Small example iOS programs in Swift 5

iOS Topics in Swift 5

Miscellaneous iOS 12 Swift 5.0 programs that implement minimal examples for various random topics.

I will blog about each examples as time permits under this section of my website: http://www.h4labs.com/dev/ios/swift_cookbook.html For now, I'll place notes in README.md files with each project.

Please note that I'm creating most of these application as "Single View Applications" then adding views (e.g. UITableView) in code. It's a personal preference to not use Storyboards.

AutoLayout Centered Button using iOS9 Anchors

Screenshot

NSLayoutConstraint.activate([
    centeredButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    centeredButton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0)
    ])

AutoLayout Two Button in StackView

Screenshot

  • Buttons are equal size with 25 points between them
  • StackView is centered horizontally
  • StackView 10 points above bottom anchor
  • Add some extra padding to button width and height
// Add some extra button padding
rightBtn.contentEdgeInsets = UIEdgeInsets.init(top: 5, left: 5, bottom: 5, right: 5)

Various UIButtons in a UIStackView

Screenshot

UIAlertController Example

Screenshot

Simple UIView Subclass

Screenshot

Gradient Layer

Screenshot

Basic UITableViewController Subclass

Screenshot

  • Subclass UITableViewController
  • Custom Header Height
  • Set Header Cell Color
  • Fixed Row Height: tableView.rowHeight = 80

Basic UITableViewController with Custom UITableViewCell

Screenshot

UITableViewController Subclass with Sections

Screenshot

UITableViewController Subclass with Sections and an Index

Screenshot

Custom UITableView Header

Screenshot

UICollectionViewController Subclass

Screenshot

UICollectionViewController Delegate

Screenshot

UICollectionView with Custom Cell

Screenshot

UISwitch

Screenshot

UISegmentedControl

Screenshot

CAShapeLayer with Basic Shapes

Screenshot

  • CAShapeLayer() added to a UIViewController
  • Use UIBezierPath to draw shapes

Animate the Drawing of a Bezier Line

Screenshot

Animate the Drawing of a Bezier Circle

Screenshot

Transition from One View to Another with a Curl Up

Screenshot

Transition from One View with UILabel to Another with a Curl Up

Screenshot

Single Tap Gesture on View

Screenshot

Simple AVAudioPlayer Example

Screenshot

Size, Rotate, and Fade Transforms

Screenshot

func rotateIt() {
    UIView.animate(withDuration: 2,
        delay: 0,
        options: .curveEaseInOut,
        animations: {

            let transform = CGAffineTransform.identity.rotated(by: .pi)

            self.label.transform = CGAffineTransform(rotationAngle: .pi)

            self.aView.transform = transform
        }, completion: {_ in

            self.fadeIt()
        })
    }

Simple UIView Subclass

Screenshot

A StackView in a StackView

Screenshot

Programmatically Show/Push ViewController

Screenshot

func nextController(_ sender:UIButton) {
    let secondViewController = SecondViewController()

    self.navigationController?.pushViewController(secondViewController, animated: true)
}
func previousController(_ sender:UIButton) {

    _ = self.navigationController?.popViewController(animated: true)
}

No Nib Project - All Code

Screenshot

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    self.window = UIWindow(frame: UIScreen.main.bounds)
    let navController = UINavigationController()
    self.window?.rootViewController = navController
    let topLevelController = ViewController()
    navController.addChildViewController(topLevelController)
    
    self.window?.makeKeyAndVisible()
    
    return true
}

UIPickerView

Screenshot

Countdown Timer

Screenshot

 timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)

Core Data

Game Clock

WebView

AppRotation

  • Misc Notes

ImageMagick

  • Fix Mac OS installation error
  • http://stackoverflow.com/questions/22715738/imagemagick-error
convert ./screenshot.png -resize 25% screenshot-small.png; # Smaller screenshot
convert ./screenshot.png -resize 20% screenshot-toc.png; # Table of Contents screenshot

Ideas and In-Progress

  • Handle Device Rotation
  • Pan Gesture|https://github.com/melling/ios_topics/blob/master/PanGesture/PanGesture
  • PDF Creation
  • Save Image to Photos
  • Dispatch Async
  • Core Data
  • sqlite|
  • Read plist

Old

AutoLayout Centered Button

Screenshot