2010-07-02 87 views
1

我有一個uitabbarcontroller,它有一個UIViewController添加到它。 (我儘量減少到最小的情況)。這個視圖控制器調用一個UIView,它通過以下方式繪製一個UIButton:UITabbarController沒有正確繪製視圖

- (void)drawRect:(CGRect)rect 
{ 
    CGRect brect = CGRectMake(0, 330, 60, 27); 
    CGPoint centern=CGPointMake(CGRectGetMidX(brect), CGRectGetMidY(brect)); 
    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setFrame:brect]; 
    [button setCenter:centern]; 
    [button setTitle:@"New" forState:UIControlStateNormal]; 
    [self addSubview:button]; 
} 

該按鈕繪製時沒有內部文本。只有當我觸摸顯示屏時,它才能正確繪製。

我的解決方案如下:給Tab Bar控制器兩個UIViewControllers來控制。然後迫使第二種的平局,然後再回到第一個:

tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:1]; 
tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:0]; 

此解決方案,但它是一個愚蠢的黑客。有一個更好的方法嗎?

+1

UIView上的哪個方法用於初始化和添加按鈕? – 2010-07-02 13:52:43

回答

-1

您發佈的代碼看起來不適合drawRect:實現。您應該將此代碼添加到您的視圖init實現中,或者添加到您的viewControllers loadView或viewDidLoad實現中。

這兩條線沒有做任何有用的東西,你可以不在:

CGPoint centern=CGPointMake(CGRectGetMidX(brect), CGRectGetMidY(brect)); 
[button setCenter:centern]; 

每當你在視圖上設置一個框架,視圖中心隱式設置到幀中點(中心)。

+0

downvote沒有評論?我錯在哪裏? – tonklon 2010-07-02 20:49:40