2010-09-13 57 views
8

我可能在這裏做錯了什麼,因爲這看起來有點愚蠢。
我在我的UINavigationController上設置了一個自定義titleView(以UILabel的形式),它在每個頁面上都是相同的。爲了促進這一點,我在我的應用程序委託中創建了一個函數來正確顯示標籤。然後,在我將其推入導航堆棧之後,我會在任何子視圖上調用此函數。
下面的代碼(這可能更有意義比我的解釋):UINavigationController上的自定義titleView動畫不正確

//In MyAppDelegate.m: 
- (void)showTitleForNavigationController:(UINavigationController*) navController { 
    UILabel *label = [[UILabel alloc] init]; 
    // set up label attributes 
    // ... 
    [label sizeToFit]; //without this line my label won't show at all 
    [navController.navigationBar.topItem setTitleView:label]; 
    [label release]; 
} 

// In SomeViewController.m, when pushing another controller onto the stack: 
    UIViewController *otherViewController = //initialize other view controller; 
    [self.navigationController pushViewController:otherViewController animated:YES]; 
    [(MyAppDelegate*)[[UIApplication sharedApplication] delegate] showTitleForNavigationController:otherViewController.navigationController]; 

我的問題是,當我推下一個視圖控制器壓入堆棧,以及新的控制器滑過順利,整個持續時間在動畫結束後,標籤貼到左上角,最後在最後貼緊到位。它看起來非常奇怪和醜陋。 如何正確設置標籤,使其從下一個視圖平滑滑動?當然,這是簡單的,我失蹤了......

回答

0

我最終做的是使用一個圖像與包含的文本作爲標題的背景,所以,而不是動畫順利,因爲我最初想,它不是動畫所有。
考慮到它到處都是同一個標題,但它並不是什麼大不了的。

3

這個問題的答案很晚,但我遇到了同樣的問題,並找到了另一種解決方法,無需使用圖像。以爲我會分享我的解決方案,因爲它可能有助於某人。

在我的情況下,我設置了一個自定義的UILabel作爲titleview,並且我意識到只有當我在viewDidLoad方法中設置titleview屬性時,它才能正確動畫。但是,在某些情況下,我並不知道我的viewDidLoad中的標題(在某些情況下,我需要使用來自http請求的標題)。所以,我對這些案例的解決方案是在viewDidLoad中使用text @「」將titleview屬性設置爲我的自定義標籤,每當我得到真正的標題時,我只更改自定義標籤的文本屬性。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    //set temporary title, the MBMUINavigationBarTitleView is a UIView subclass whose viewWithTitle method returns an autoreleased UIlabel with my custom settings, custom font etc. 
    self.navigationItem.titleView = [MBMUINavigationBarTitleView viewWithTitle:@" "]; 
} 

//somewhere later, when I have the real title 
UILabel* titleLabel = (UILabel*)self.navigationItem.titleView; 
[titleLabel setText:theRealTitle]; 
+0

titleLabel是否會自行調整大小? – leftspin 2012-01-15 06:05:21

+0

如果我沒有記錯的話,我有一個函數可以在文本被改變時調整標籤的大小。 – ylva 2012-01-16 17:34:47

0

我是在一個類似的情況ylva,使用自定義的文本類的實例爲UINavigationItem'stitleView財產。但是,我發現在viewDidLoad中配置它沒有解決動畫故障。

我對這個問題的解決方法是等到問題的視圖控制器被彈出導航控制器的堆棧,並在那一點刪除UINavigationItem's自定義titleView,所以它永遠不需要動畫。

當我UINavigationController子類接收的popViewControllerAnimated:消息,我從我的自定義文本字段(UINavigationItem'stitleView)複製標題文本到UINavigationItem'stitle屬性和設置titleView屬性爲零。然後UINavigationController繼續,並從視圖控制器彈出,只有標準導航欄標題標籤是動畫(不是我的自定義標題),毛刺自由。