Search code examples
iosrubymotion

How to insert image into bar created by UIViewController?


The tabs can be created - however, how can you insert a graphic into each tab and when the tabs are selected, how do you call it out to a new screen?

# Create a tabbar
tabbar = UITabBarController.alloc.init

# Each Tab has a view controller allocated

tabbar.viewControllers = [
    UIViewController.alloc.init,
    UIViewController.alloc.init, 
    UIViewController.alloc.init,
    UIViewController.alloc.init
]

# Selected index to 0
tabbar.selectedIndex = 0

# The root view controller also changed
@window.rootViewController = UINavigationController.alloc.initWithRootViewController(tabbar)

# We will force to use a full screen layout.
@window.rootViewController.wantsFullScreenLayout = true

Solution

  • See the "Beers" sample project at https://github.com/HipByte/RubyMotionSamples

    You're on the right track, the only difference is that you'll want to list your specific view controller classes instead of the generic UIViewController class. From the sample:

    tabbar.viewControllers = [BeerMapController.alloc.init,
                              BeerListController.alloc.init]
    

    Then in each of those controllers' init methods, you set the tabBarItem to give it a title and image:

    class BeerListController < UITableViewController
      def init
        if super
          self.tabBarItem = UITabBarItem.alloc.initWithTitle('List', image:UIImage.imageNamed('list.png'), tag:1)
        end
        self
      end
    end
    

    Alternatively, if you don't have specific view controllers yet, I imagine you could do something like this:

    tabbar.viewControllers = [
      UIViewController.alloc.init,
      UIViewController.alloc.init
    ]
    tabbar.viewControllers[0].tabBarItem = UITabBarItem.alloc.initWithTitle('List', image:UIImage.imageNamed('list.png'), tag:1)
    # etc.