Monday, April 25, 2016

UIBarButtonItem types and UIToolbar color settings

This post shows the different UIBarButtonItem types in iOS as well as different UIToolbar color settings. The code is written in Swift 2.2 with Xcode 7.3.

Modify ViewController.swift as:


import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.yellowColor()
        
        let myColor = UIColor(red: 55.0/255.0, green: 210.0/255.0, blue: 200.0/255.0, alpha: 1.0)
        
        //Bar Button Items
        let action = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Action, target: self, action: nil)
        let add = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: nil)
        let bookmarks = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Bookmarks, target: self, action: nil)
        let camera = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Camera, target: self, action: nil)
        let cancel = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: self, action: nil)
        let compose = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Compose, target: self, action: nil)
        let done = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: nil)
        let edit = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Edit, target: self, action: nil)
        let fastForward = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target: self, action: nil)
        let organize = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Organize, target: self, action: nil)
        let pause = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: nil)
        let play = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: nil)
        let redo = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Redo, target: self, action: nil)
        let refresh = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target: self, action: nil)
        let reply = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Reply, target: self, action: nil)
        let rewind = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Rewind, target: self, action: nil)
        let save = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Save, target: self, action: nil)
        let search = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: nil)
        let stop = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Stop, target: self, action: nil)
        let trash = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Trash, target: self, action: nil)
        let undo = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Undo, target: self, action: nil)
        let flexible = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
        
        //Bar Button Items in arrays
        let items1 = [action, flexible, add]
        let items2 = [bookmarks, flexible, camera]
        let items3 = [cancel, flexible, compose]
        let items4 = [done, flexible, edit]
        let items5 = [fastForward, flexible, organize]
        let items6 = [pause, flexible, play]
        let items7 = [redo, flexible, refresh]
        let items8 = [reply, flexible, rewind]
        let items9 = [save, flexible, search]
        let items10 = [stop, flexible, trash, flexible, undo]
        
        //Toolbar 1 - Background Color + No Translucent
        let toolbar1 = UIToolbar()
        toolbar1.sizeToFit()
        toolbar1.center = CGPointMake(view.frame.width/2, 50)
        toolbar1.backgroundColor = myColor
        toolbar1.translucent = false
        toolbar1.items = items1
        
        //Toolbar 2 - Background Color
        let toolbar2CenterY : CGFloat = toolbar1.center.y + toolbar1.frame.height
        let toolbar2 = UIToolbar()
        toolbar2.sizeToFit()
        toolbar2.center = CGPointMake(view.frame.width/2, toolbar2CenterY)
        toolbar2.backgroundColor = myColor
        toolbar2.items = items2
        
        //Toolbar 3 - Bar Tint Color
        let toolbar3CenterY : CGFloat = toolbar2.center.y + toolbar2.frame.height
        let toolbar3 = UIToolbar()
        toolbar3.sizeToFit()
        toolbar3.center = CGPointMake(view.frame.width/2, toolbar3CenterY)
        toolbar3.barTintColor = myColor
        toolbar3.items = items3
        
        //Toolbar 4 - Bar Tint Color + No Translucent
        let toolbar4CenterY : CGFloat = toolbar3.center.y + toolbar3.frame.height
        let toolbar4 = UIToolbar()
        toolbar4.sizeToFit()
        toolbar4.center = CGPointMake(view.frame.width/2, toolbar4CenterY)
        toolbar4.barTintColor = myColor
        toolbar4.translucent = false
        toolbar4.items = items4
        
        //Toolbar 5 - Bar Tint Color + No Translucent + White Tint Color
        let toolbar5CenterY : CGFloat = toolbar4.center.y + toolbar4.frame.height
        let toolbar5 = UIToolbar()
        toolbar5.sizeToFit()
        toolbar5.center = CGPointMake(view.frame.width/2, toolbar5CenterY)
        toolbar5.barTintColor = myColor
        toolbar5.translucent = false
        toolbar5.tintColor = UIColor.whiteColor()
        toolbar5.items = items5
        
        //Toolbar 6 - Background Color + No Translucent
        let toolbar6CenterY : CGFloat = toolbar5.center.y + toolbar5.frame.height+50
        let toolbar6 = UIToolbar()
        toolbar6.sizeToFit()
        toolbar6.center = CGPointMake(view.frame.width/2, toolbar6CenterY)
        toolbar6.backgroundColor = UIColor.orangeColor()
        toolbar6.translucent = false
        toolbar6.items = items6
        
        //Toolbar 7 - Background Color
        let toolbar7CenterY : CGFloat = toolbar6.center.y + toolbar6.frame.height
        let toolbar7 = UIToolbar()
        toolbar7.sizeToFit()
        toolbar7.center = CGPointMake(view.frame.width/2, toolbar7CenterY)
        toolbar7.backgroundColor = UIColor.orangeColor()
        toolbar7.items = items7
        
        //Toolbar 8 - Bar Tint Color
        let toolbar8CenterY : CGFloat = toolbar7.center.y + toolbar7.frame.height
        let toolbar8 = UIToolbar()
        toolbar8.sizeToFit()
        toolbar8.center = CGPointMake(view.frame.width/2, toolbar8CenterY)
        toolbar8.barTintColor = UIColor.orangeColor()
        toolbar8.items = items8
        
        //Toolbar 9 - Bar Tint Color + No Translucent
        let toolbar9CenterY : CGFloat = toolbar8.center.y + toolbar8.frame.height
        let toolbar9 = UIToolbar()
        toolbar9.sizeToFit()
        toolbar9.center = CGPointMake(view.frame.width/2, toolbar9CenterY)
        toolbar9.barTintColor = UIColor.orangeColor()
        toolbar9.translucent = false
        toolbar9.items = items9
        
        //Toolbar 10 - Bar Tint Color + No Translucent + White Tint Color
        let toolbar10CenterY : CGFloat = toolbar9.center.y + toolbar9.frame.height
        let toolbar10 = UIToolbar()
        toolbar10.sizeToFit()
        toolbar10.center = CGPointMake(view.frame.width/2, toolbar10CenterY)
        toolbar10.barTintColor = UIColor.orangeColor()
        toolbar10.translucent = false
        toolbar10.tintColor = UIColor.whiteColor()
        toolbar10.items = items10
        
        view.addSubview(toolbar1)
        view.addSubview(toolbar2)
        view.addSubview(toolbar3)
        view.addSubview(toolbar4)
        view.addSubview(toolbar5)
        view.addSubview(toolbar6)
        view.addSubview(toolbar7)
        view.addSubview(toolbar8)
        view.addSubview(toolbar9)
        view.addSubview(toolbar10)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

Result:



Related Information:

Adding UIBarButtonItem to UIToolbar with left/right alignment

No comments:

Post a Comment