How to change state images for tabbar items?

孤街醉人 提交于 2019-12-11 15:55:44

问题


I am building a Rubymotion app and I am customizing the tabBar. I have managed to put a custom image as a background of the tabBar but now I need to set individual images to each tab. One for when it is pressed and one for when it is not.

I am following the guide (for objective-c) at NSScreencasts.com and the show notes says I should use this code. But when I try it in Ruby (which I think is correct) I get an error.

In Objective-C:

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Friends"
                                                        image:nil
                                                          tag:0];
        [self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"tabbar-activity-selected.png"]
                      withFinishedUnselectedImage:[UIImage imageNamed:@"tabbar-activity.png"]];
    }
    return self;
}

My Ruby code:

class FirstController < UIViewController
  def viewDidLoad
    super

    view.backgroundColor = UIColor.whiteColor 

    self.tabBarItem = UITabBarItem.alloc.initWithTitle('Friends', image: nil, tag: 0)
    self.tabBarItem.setFinishedSelectedImage(UIImage.imageNamed('tabitem_selected.png'))
    self.tabBarItem.withFinishedUnselectedImage(UIImage.imageNamed('tabitem.png')) 
  end
end

The error:

first_controller.rb:8:in `viewDidLoad': undefined method `setFinishedSelectedImage' for #<UITabBarItem:0x6b71670> (NoMethodError)
    from app_delegate.rb:7:in `application:didFinishLaunchingWithOptions:'
2012-11-16 14:45:56.924 custom_tabbar[45679:f803] *** Terminating app due to uncaught exception 'NoMethodError', reason: 'first_controller.rb:8:in `viewDidLoad': undefined method `setFinishedSelectedImage' for #<UITabBarItem:0x6b71670> (NoMethodError)

Also. Is it really correct to set this code in the viewDidLoad?


回答1:


These lines in Objective-C are one method:

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"tabbar-activity-selected.png"]
              withFinishedUnselectedImage:[UIImage imageNamed:@"tabbar-activity.png"]];

The signature is:

- (void)setFinishedSelectedImage:(UIImage *)selectedImage 
     withFinishedUnselectedImage:(UIImage *)unselectedImage

So for RubyMotion, the method signature is:

setFinishedSelectedImage(image, withFinishedUnselectedImage:image)

Which converts to this for you:

self.tabBarItem.setFinishedSelectedImage(UIImage.imageNamed('tabitem_selected.png'),
                                         withFinishedUnselectedImage: UIImage.imageNamed('tabitem.png'))


来源:https://stackoverflow.com/questions/13417873/how-to-change-state-images-for-tabbar-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!