2012-11-16 2 views
0

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 
+0

'self.backgroundImage = tabbarBg' 또는'self.setBackgroundImage (tabbarBg)'중 하나를 시도해 보셨습니까? – kuba

+0

예, 둘 다 실패합니다. –

+0

또한'UITabBarController' (컨트롤러)라는 서브 클래스에있는 문제를 보았습니다. 대신 UITabBar (UIView) 서브 클래스를 만들어야합니다. – kuba

답변

1

에 따르면, "채팅"우리가했다 당신이보기와 컨트롤러의 적절한 계층 구조에 대해 매우 혼란스러워 날 것으로 보인다. 컨트롤러는 뷰를 소유하지만 컨트롤러에는 시각적 속성이 없습니다. 보기에는 시각적 인 사항 (예 : 배경 이미지)이 있습니다. 그래서 예. a 탭 막대가있을 때 실제로는 : 1) TabBarController 2) TabBar (보기).

이제 TabBar는보기이며 배경을 변경할 수있는 "backgroundImage"라는 속성이 있습니다. 그 중에서도 TabBarController에는 그런 것이 없지만 "내부"컨트롤러 목록이 있습니다.

원하는 코드를 보여 드리겠습니다. 그것은 Obj-C이지만, Ruby로 다시 작성하는 것은 간단해야합니다. 내 AppDelegate에의 didFinishLaunchingWithOptions 방법이 있습니다

UITabBarController *tbc = [[UITabBarController alloc] init]; 

UIViewController *v1 = [[UIViewController alloc] init]; 
UIViewController *v2 = [[UIViewController alloc] init]; 

tbc.viewControllers = @[v1, v2]; 
tbc.tabBar.backgroundImage = [UIImage imageNamed:@"a.png"]; 

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
self.window.rootViewController = tbc; 
self.window.backgroundColor = [UIColor whiteColor]; 
[self.window makeKeyAndVisible]; 
return YES; 

공지 사항의 TabBarController는 "viewControllers"속성을 가지고 -이 내부 컨트롤러의 목록입니다. UITabBar 뷰에 대한 참조 인 "tabBar"속성도 있습니다. 나는 그것에 접근하고 배경 이미지를 설정한다.

+0

절대적으로 뛰어납니다. 이걸 도와 주셔서 감사합니다! –