How to set image in a tab bar item in Swift? - ios

I have taken a view controller & embedded it in a navigation Controller and again this has been embedded in a tab bar controller. when I am trying to set a image via story board, the image does not appear on a tab bar icon. Here image name is 25.
What can I do? How can I do it programmatically? what should I take proper image size for this purpose?

In your MainTabbarViewController
Bind the outlet of your tabbar:
#IBOutlet weak var myTabBar: UITabBar?
override func viewDidLoad() {
super.viewDidLoad()
myTabBar?.tintColor = UIColor.white
tabBarItem.title = ""
setTabBarItems()
}
set the tabbar items here defined method below:
func setTabBarItems(){
let myTabBarItem1 = (self.tabBar.items?[0])! as UITabBarItem
myTabBarItem1.image = UIImage(named: "Unselected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem1.selectedImage = UIImage(named: "Selected ")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem1.title = ""
myTabBarItem1.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
let myTabBarItem2 = (self.tabBar.items?[1])! as UITabBarItem
myTabBarItem2.image = UIImage(named: "Unselected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem2.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem2.title = ""
myTabBarItem2.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
let myTabBarItem3 = (self.tabBar.items?[2])! as UITabBarItem
myTabBarItem3.image = UIImage(named: "Unselected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem3.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem3.title = ""
myTabBarItem3.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
let myTabBarItem4 = (self.tabBar.items?[3])! as UITabBarItem
myTabBarItem4.image = UIImage(named: "Unselected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem4.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem4.title = ""
myTabBarItem4.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
}

add AppDelegate class :
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
window=UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = setTabbar()
self.window?.makeKeyAndVisible()
window?.backgroundColor=UIColor.white
return true
}
func setTabbar() -> UITabBarController
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabbarcntrl = UITabBarController()
let Home = storyboard.instantiateViewController(withIdentifier: "HomeView") // 1st tab bar viewcontroller
let Followed = storyboard.instantiateViewController(withIdentifier: "FollowedView") // 2nd tab bar viewcontroller
let Message = storyboard.instantiateViewController(withIdentifier: "MessageView") // 3rd tab bar viewcontroller
// all viewcontroller embedded navigationbar
let nvHome = UINavigationController(rootViewController: Home)
let nvFollowed = UINavigationController(rootViewController: Followed)
let nvMessage = UINavigationController(rootViewController: Message)
// all viewcontroller navigationbar hidden
nvHome.setNavigationBarHidden(true, animated: false)
nvFollowed.setNavigationBarHidden(true, animated: false)
nvMessage.setNavigationBarHidden(true, animated: false)
tabbarcntrl.viewControllers = [nvHome,nvFollowed,nvMessage]
let tabbar = tabbarcntrl.tabBar
tabbar.barTintColor = UIColor.black
tabbar.backgroundColor = UIColor.black
tabbar.tintColor = UIColor(red: 43/255, green: 180/255, blue: 0/255, alpha: 1)
//UITabBar.appearance().tintColor = UIColor.white
let attributes = [NSFontAttributeName:UIFont(name: "Montserrat-Light", size: 10)!,NSForegroundColorAttributeName:UIColor.white]
let attributes1 = [NSFontAttributeName:UIFont(name: "Montserrat-Light", size: 10)!,NSForegroundColorAttributeName:UIColor(red: 43/255, green: 180/255, blue: 0/255, alpha: 1)]
UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .normal)
UITabBarItem.appearance().setTitleTextAttributes(attributes1, for: .selected)
let tabHome = tabbar.items![0]
tabHome.title = "Home" // tabbar titlee
tabHome.image=UIImage(named: "icon_home.png")?.withRenderingMode(.alwaysOriginal) // deselect image
tabHome.selectedImage = UIImage(named: "icon_home.png")?.withRenderingMode(.alwaysOriginal) // select image
tabHome.titlePositionAdjustment.vertical = tabHome.titlePositionAdjustment.vertical-4 // title position change
let tabFoll = tabbar.items![1]
tabFoll.title = "Followed"
tabFoll.image=UIImage(named: "icon_fold.png")?.withRenderingMode(.alwaysOriginal)
tabFoll.selectedImage=UIImage(named: "icon_fold.png")?.withRenderingMode(.alwaysOriginal)
tabFoll.titlePositionAdjustment.vertical = tabFoll.titlePositionAdjustment.vertical-4
let tabMsg = tabbar.items![3]
tabMsg.title = "Message"
tabMsg.image=UIImage(named: "icon_mail.png")?.withRenderingMode(.alwaysOriginal)
tabMsg.selectedImage=UIImage(named: "icon_mail.png")?.withRenderingMode(.alwaysOriginal)
tabMsg.titlePositionAdjustment.vertical = tabMsg.titlePositionAdjustment.vertical-4
return tabbarcntrl
}

Set both images- for select/selected state

You are doing all the things in right way But the only problem is your tabbaritem image is not in correct size .Just look this table for actual size of tabbaritem images.

In swift 4 and 5 you can use the below extension. Remember one thing always pass the same number of images , selected images and title but if you do not want to set title then pass nil in title.
extension UITabBarController{
func setUpImagaOntabbar(_ selectedImage : [UIImage], _ image : [UIImage], _ title : [String]?){
for (index,vals) in image.enumerated(){
if let tab = self.tabBar.items?[index]{
tab.image = image[index]
tab.image = selectedImage[index]
if let tile = title[index]{
tab.title = title[index]
}
}
}
}
}

Related

UINavigationBar Large Title doesn't appear when scroll view up

I have implemented a feature, when you press on a UITabBar icon and viewController1 scrolls up using its UIScrollView. It works perfectly, but if I scroll view down and stop somewhere, then switch to another viewController2, then get back to viewController1 and press tabBar icon - the viewController1 will scroll up, but Large Title will never be showed, and I should press tabBar icon one more time to show it:
The code I use for scroll up the VC1:
private var biggestTopSafeAreaInset: CGFloat = 0
override func viewSafeAreaInsetsDidChange() {
super.viewSafeAreaInsetsDidChange()
self.biggestTopSafeAreaInset = max(view.safeAreaInsets.top, biggestTopSafeAreaInset)
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if tabBarController.selectedIndex == 0 {
let navigationVC = viewController as? UINavigationController
let firstVC = navigationVC?.viewControllers.first as? CurrencyViewController
guard let scrollView = firstVC?.view.subviews.first(where: { $0 is UIScrollView }) as? UIScrollView else { return }
if traitCollection.verticalSizeClass == .compact {
scrollView.setContentOffset(CGPoint(x: 0, y: -view.safeAreaInsets.top, animated: true)
} else {
scrollView.setContentOffset(CGPoint(x: 0, y: -biggestTopSafeAreaInset, animated: true)
}
}
}
I tried to track biggestTopSafeAreaInset in different stages of VC1 life, but it always has the same number - 196.0. But then why it doesn't scroll till the Large Title after viewControllers switch?
in your tableView set contentInsetAdjustmentBehavior to never
tableView.contentInsetAdjustmentBehavior = .never
in controller update the ui of navigation bar again
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DispatchQueue.main.async { [weak self] in
self?.navigationController?.navigationBar.sizeToFit()
}
}
here is the navigation controller
class BaseNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 15.0, *) {
let scrollAppearance = UINavigationBarAppearance()
scrollAppearance.shadowColor = .white
scrollAppearance.backgroundColor = .white
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithDefaultBackground()
navigationBarAppearance.backgroundColor = .white
navigationBarAppearance.largeTitleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 26),
NSAttributedString.Key.foregroundColor: UIColor.black
]
navigationBarAppearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17),
NSAttributedString.Key.foregroundColor: UIColor.black
]
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "back-arrow")
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = scrollAppearance
navigationBar.tintColor = .black
navigationBar.prefersLargeTitles = true
navigationBar.isTranslucent = false
navigationItem.largeTitleDisplayMode = .automatic
} else {
navigationBar.largeTitleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 26),
NSAttributedString.Key.foregroundColor: UIColor.black
]
navigationBar.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17),
NSAttributedString.Key.foregroundColor: UIColor.black
]
navigationBar.tintColor = .black
navigationBar.prefersLargeTitles = true
navigationBar.isTranslucent = false
navigationItem.largeTitleDisplayMode = .automatic
navigationBar.barTintColor = .white
}
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .darkContent
}
}
here is the Tabbar Controller
class TabbarController:UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let c1 = C1()
let c2 = C2()
let c3 = C3()
c1.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "home786"), tag: 0)
c1.tabBarItem.tag = 0
let nav1 = BaseNavigationController(rootViewController: c1)
c2.tabBarItem = UITabBarItem(title: "Setting", image: UIImage(named: "home786"), tag: 0)
c2.tabBarItem.tag = 1
let nav2 = BaseNavigationController(rootViewController: c2)
c2.tabBarItem = UITabBarItem(title: "User", image: UIImage(named: "home786"), tag: 0)
c2.tabBarItem.tag = 2
let nav3 = BaseNavigationController(rootViewController: c3)
viewControllers = [nav1,nav2,nav3]
selectedViewController = nav1
tabBarController?.viewControllers?.first?.view.backgroundColor = .red
}
}
Try to add this in viewDidLoad:
view.addSubview(UIView())
this single line block large title navigation Bar... I don't Know why, but this trick fix momentarily the issue...
After some research I found out what can fix my problem. If you call this method with a small delay in tabBarController didSelect then it will be possible to see a Large Title after switching viewControllers. But I still can't figure out exactly why it happened...
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
navigationVC?.navigationBar.sizeToFit()
}

Change navigation bar color in QLPreviewController

I want to change navigation bar color of QLPreviewController in swift 3. I have used below code to change color but its not working
viewQLPreview = QLPreviewController()
viewQLPreview.dataSource = self
viewQLPreview.delegate = self
viewQLPreview.navigationController?.navigationBar.isTranslucent = false
viewQLPreview.navigationController?.navigationBar.tintColor = UIColor.red
I have used below code to Change navigation bar color of QLPreviewController in swift 3.0
UINavigationBar.appearance().barTintColor = UIColor.red
UINavigationBar.appearance(whenContainedInInstancesOf: [QLPreviewController.self]).backgroundColor = UIColor.red
Use below code before present QLPreviewController :
UINavigationBar.appearance().tintColor = UIColor.red
UINavigationBar.appearance().barTintColor = UIColor.blue
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]
UINavigationBar.appearance().setBackgroundImage(fromColor(color: UIColor.blue), for: .default)
UINavigationBar.appearance().isTranslucent = false
func fromColor (color: UIColor) -> UIImage{
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context: CGContext? = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(rect)
let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image ?? UIImage()
}
Put below code in viewDidLoad of QLPreviewController.
viewQLPreview.navigationController?.navigationBar.isTranslucent = false
viewQLPreview.navigationController?.navigationBar.tintColor = UIColor.red
Also make sure that viewQLPreview.navigationController != nil
If you are pushing QLPreviewController then this code will work....
If you are presenting QLPreviewController then you need to make sure that rootController should be navigation controller, in your case..
let viewQLPreview = QLPreviewController()
let nav = UINavigationController(rootViewController: viewQLPreview)
self.present(nav, animated: true, completion: nil)

Accessing functions/variables inside a UITabBarController with or without global variables?

currently I have a layout like this with my app. I have a UITabBarcontroller as my root view controller inside app delegate, this is fine and works great.
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FIRApp.configure()
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = Tabs()
UITabBar.appearance().tintColor = UIColor.init(r: 198, g: 214, b: 91)
UITabBar.appearance().barTintColor = UIColor.init(r: 47, g: 47, b: 47)
UINavigationBar.appearance().barTintColor = UIColor.init(r: 53, g: 57, b: 77)
UINavigationBar.appearance().tintColor = UIColor.init(r: 198, g: 214, b: 91)
However, in my layout with the tab controller, I have laid it out in a way where i have UITabBar at the top, followed by an override didload function that lays out all the views like this.
class Tabs: UITabBarController{
override func viewDidLoad() {
super.viewDidLoad()
tabBar.isTranslucent = true
let feed = feedController()
let feedArray = UINavigationController(rootViewController: feed)
let feedButton = UITabBarItem(title: nil, image: UIImage(named: "feed.png"), selectedImage: UIImage(named: "selectedimage.png"))
feedButton.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
feedArray.tabBarItem = feedButton
let messages = messagesController()
let messagesArray = UINavigationController(rootViewController: messages)
let messagesButton = UITabBarItem(title: nil, image: UIImage(named: "messages.png"), selectedImage: UIImage(named: "selectedimage.png"))
messagesButton.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
messagesArray.tabBarItem = messagesButton
let post = postController()
let postButton = UITabBarItem(title: nil, image: UIImage(named: "post.png"), selectedImage: UIImage(named: "selectedimage.png"))
postButton.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
post.tabBarItem = postButton
let profile = profileController()
let profileArray = UINavigationController(rootViewController: profile)
let profileButton = UITabBarItem(title: nil, image: UIImage(named: "profile.png"), selectedImage: UIImage(named: "selectedimage.png"))
profileButton.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
profile.tabBarItem = profileButton
let settings = settingsController()
let settingsArray = UINavigationController(rootViewController: settings)
let settingsButton = UITabBarItem(title: nil, image: UIImage(named: "settings.png"), selectedImage: UIImage(named: "selectedimage.png"))
settingsButton.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
settingsArray.tabBarItem = settingsButton
if FIRAuth.auth()?.currentUser?.uid == nil {
DispatchQueue.main.async {
self.handleLogout()}
}
/* all views BOII */ viewControllers = [feedArray, messagesArray, post, profileArray, settingsArray]
However, when I try to access a function or whatever in a viewcontroller inside this tab controller from another viewcontroller inside. like this
class settingsController: UIViewController{
messagesController().removeMessages()
}
.......
class messagesController: UIViewController{
removeMessages(){
messages.removeAll()
messagesDictionary.removeAll()
tableView.reloadData()
print("working")
}
}
The functions get called (im putting it in a function on logout), I get the print message but anything I wanted affected on that viewcontroller does not seem to work. Currently, I have so far tried these methods (on settings controller).
I know this has to do with something with a tabbarcontroller in swift, something about how the views inside run parallel to each other.
What I did do and it worked, was put the declarations of the variables before the class in my tabs.swift file. So I took the let statements that set up the views and put them before the class was declared and under the import statements. This worked, but is this bad practice and is there a better way of doing it? As this let statements are now global.
Really would appreciate the help guys and Im so sorry for the long post, I just wanted you to try and understand what Im saying here, I'm new.
If your UIKit objects manipulates from current stack it won't work. Since object should be called from main thread.
DispatchQueue.main.async {
self.handleLogout()
}
I can only suggest you fast solution use next block for UI elements:
dispatch_async(dispatch_get_main_queue()) {
messages.removeAll()
messagesDictionary.removeAll()
tableView.reloadData()
}

How to create a tab bar using SingleViewApplication iOS?

I am new in iOS and my requirement is to implement Tab bar.
I have 5 tab bar items and for each item there is different Screen(UIViewController).
I have a HomeViewController and remaining 4 ViewControllers.
I have to show all UIViewControllers using tabBar item.
And I have to show tab bar in bottom for all UIViewControllers.
In Image You can see.
In Image, I am in HomeViewController and for each tab I have to go for respective ViewControllers but tab bar should be present for all ViewControllers.
StoryBoard :-
I read tutorials but still confused.
How can I do it?
Thanks
You can create the class of tab view controller like this
class TabBarViewController: UITabBarController {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
override func viewDidLoad() {
super.viewDidLoad()
UITabBar.appearance().tintColor = UIColor(red: 254/255.0, green: 105/255.0, blue: 51/255.0, alpha: 100.0)
for i in 0 ..< self.tabBar.items!.count {
switch i {
case 0:
tabBar.items?[0].title = "Home"
let firstTab = self.tabBar.items![i] as UITabBarItem
learnTab.image = UIImage(named: "Homedeselected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
firstTab.selectedImage = UIImage(named: "Homedeselected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
firstTab.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0)
firstTab.titlePositionAdjustment = UIOffsetMake(0, -4)
case 1:
tabBar.items?[1].title = "Request"
let secondTab = self.tabBar.items![i] as UITabBarItem
secondTab.image = UIImage(named: "RequestDeselected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
secondTab.selectedImage = UIImage(named: "Requestselected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
secondTab.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0)
secondTab.titlePositionAdjustment = UIOffsetMake(0, -4)
case 2:
tabBar.items?[2].title = "Renew"
let thirdTab = self.tabBar.items![i] as UITabBarItem
thirdTab.image = UIImage(named: "RenewDeSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
thirdTab.selectedImage = UIImage(named: "RenewSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
thirdTab.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0)
thirdTab.titlePositionAdjustment = UIOffsetMake(0, -4)
case 3:
tabBar.items?[3].title = "ReSell"
let forthTab = self.tabBar.items![i] as UITabBarItem
forthTab.image = UIImage(named: "ReSellDeSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
forthTab.selectedImage = UIImage(named: "ReSellSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
forthTab.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0)
forthTab.titlePositionAdjustment = UIOffsetMake(0, -4)
case 4:
tabBar.items?[4].title = "ReCycle"
let fifthTab = self.tabBar.items![i] as UITabBarItem
fifthTab.image = UIImage(named: "ReCycleDeSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
fifthTab.selectedImage = UIImage(named: "ReCycleSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
fifthTab.imageInsets = UIEdgeInsetsMake(-2, 0, 2, 0)
fifthTab.titlePositionAdjustment = UIOffsetMake(0, -5)
default:
break
}
}
}
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
let rootView = self.viewControllers![self.selectedIndex] as! UINavigationController
rootView.popToRootViewControllerAnimated(ture)
}
}
}

presenting ViewController Label from UITabBarController

I'm new in programming IOS Apps with swift.
I've programed TabBar in AppDelegate.swift with the code, and with 2 ViewControllers program in MainPageViewController.swift and DetailViewController.swift.
let tabBarController = UITabBarController()
let mainPageVC = MainPageViewController()
let detailVC = DetailViewController()
tabBarController.viewControllers = [mainPageVC,detailVC]
let mainTabBar = UITabBarItem(title: nil, image: UIImage(named: "03")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "06")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal))
let detailTabBar = UITabBarItem(title: nil, image: UIImage(named: "02")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "05")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal))
mainPageVC.tabBarItem = mainTabBar
deviceVC.tabBarItem = deviceTabBar
mainPageVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
deviceVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
UITabBar.appearance().barTintColor = UIColor(red: 211/255, green: 1, blue: 246/255, alpha: 1)
window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
And then I added a Label in the MainPageViewController.swift
class MainPageViewController: UIViewController {
#IBOutlet weak var testLabel: UILabel!
#IBOutlet weak var mainTabBarItem: UITabBarItem!
override func viewDidLoad() {
super.viewDidLoad()
let backgraondLayer = color.gl
backgraondLayer.frame = view.frame
self.view.layer.insertSublayer(backgraondLayer, atIndex: 0)
testLabel.backgroundColor = UIColor.blackColor()
}
I keep getting the "fatal error: unexpectedly found nil while unwrapping an Optional value" error.
And if I remove the "Label.backgroundColor" code and setup the background color in the Attribute Inspector, the Build App will not shown the Label.
What code did i miss?
Thanks in advance!
If it gives you "nil while unwrapping an optional value" in testLabel.backgroundColor = UIColor.blackColor(), the chances are that your label is not connected to the control in Interface Builder. In the editor the IBOutlets have little circle next to the declaration. Empty one for unconnected and circle with dot inside for connected.
If you are still at the beginning of your app dev, consider doing the UITabBarController plumbing in Interface Builder/Storyboard. It will save you from some boring work every time you add a new tab.

Resources