2012-07-11 69 views
0

以下this教程,我結束了一個很好的箭頭在我的iPhone UITabBar。iPad的UITabBar箭頭指示器

現在我想爲iPad做同樣的事情,但它似乎不起作用。

我試圖

- (CGFloat) horizontalLocationFor:(NSUInteger)tabIndex 
{ 
    CGFloat tabItemWidth; 
    CGFloat halfTabItemWidth; 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){ 
     tabItemWidth = 320/tabBarController.tabBar.items.count; 
     halfTabItemWidth = (tabItemWidth/2.0) - (tabBarArrow.frame.size.width/2.0); 
    } 
    else 
    { 
     tabItemWidth = 768/tabBarController.tabBar.items.count; 
     halfTabItemWidth = (tabItemWidth/2) - (tabBarArrow.frame.size.width/2); 
    } 

    return (tabIndex * tabItemWidth) + halfTabItemWidth; 
} 

以下的例子中,採取的TabBar尺寸爲iPad,但箭頭仍然是來自每個標籤的中部被爲止。

任何幫助?

+0

對於手機/吊艙,您正在分配2.0和其他設備(墊)你分開2.在這種情況下應該沒有多大關係。一般來說,這是一種糟糕的風格,因爲它有時會出現陷阱。而這些錯誤可能很難找到。順便說一句,if和els之間的唯一真正區別是320和768.另外,你應該支持在iPad上使用橫向格式,這意味着一些稍微不同的編碼。無論如何,代碼看起來很不錯。我會NSLog或調試這個函數返回一個合適的值。如果是這樣,那麼我會看到其他錯誤。 – 2012-07-11 14:07:14

+0

我不支持橫向模式,所以不會有任何問題。奇怪的是,應該使用768,但它不是!這就是我沒有得到的。我試圖做一些測試,並通過將標籤欄項目的數量除以2,箭頭似乎不會將其移開。 – Phillip 2012-07-11 14:09:55

+0

您是否檢查過以確保您的其他語句被調用? 'NSLog'或斷點 – Dustin 2012-07-11 14:19:44

回答

1

嘗試此代替當前「horizo​​ntalLocationFor」方法的您有:

- (CGFloat) horizontalLocationFor:(NSUInteger)tabIndex 
{ 
    // Get the frame of the selected tab item's view. 
    // Add one becuase the first subview is the not a button. 
    CGRect tabFrame = [[[[[self tabBarController] tabBar] subviews] objectAtIndex:tabIndex+1] frame]; 

    // Add tab x to half tab width and substract hald arrow image width to center it. 
    return tabFrame.origin.x + (tabFrame.size.width * .5) - (tabBarArrow.frame.size.width * .5); 
} 

原始溶液假設標籤欄寬度爲整個屏幕,但在ipad的4項僅填充的一個部分標籤欄寬度,因此將寬度除以項目數量不會使您獲得正確的位置。

+0

工作正常!太好了,非常感謝你!沒有得到你所說的完美。 – Phillip 2012-07-11 14:51:28