我在写一个Rubymotion应用程序,我想自定义TabBar。在NSScreencasts.com上,他们解释了如何在Objective-C中完成它,但是如何将下面的代码转换成Ruby?如何将自定义背景图像设置为tabbar?
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self customize];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self customize];
}
return self;
}
- (void)customize {
UIImage *tabbarBg = [UIImage imageNamed:@"tabbar-background.png"];
UIImage *tabBarSelected = [UIImage imageNamed:@"tabbar-background-pressed.png"];
[self setBackgroundImage:tabbarBg];
[self setSelectionIndicatorImage:tabBarSelected];
}
@end
这是我的尝试:
class CustomTabbar < UITabBarController
def init
super
customize
self
end
def customize
tabbarBg = UIImage.imageNamed('tabbar.jpeg')
self.setBackgroundImage = tabbarBg
end
end
但是,如果我运行它,我得到这个错误:
Terminating app due to uncaught exception 'NoMethodError', reason: 'custom_tabbar.rb:5:in `init': undefined method `setBackgroundImage=' for #<CustomTabbar:0x8e31a70> (NoMethodError)
UPDATE
*这是我app_delete文件:*
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
first_controller = FirstController.alloc.init
second_controller = SecondController.alloc.init
tabbar_controller = CustomTabbar.alloc.init
tabbar_controller.viewControllers = [first_controller, second_controller]
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@window.rootViewController = tabbar_controller
@window.makeKeyAndVisible
true
end
end
你有没有尝试过'self.backgroundImage = tabbarBg'或'self.setBackgroundImage(tabbarBg)'? – kuba
是的,都失败了。 –
另外我看到一个问题,你子类'UITabBarController'(这是一个控制器),但相反,你应该继承'UITabBar'(这是一个UIView) – kuba