我在寫一個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