1

這是我的困境。因爲我在Objective-C和iOS編程方面有一點小菜,所以和我一起B B。我有一個leftBarButtonItem(使用自定義視圖),可以替代後退按鈕(因爲自定義視圖不能在backBarButtonItem上使用)。我也有一個rightBarButtonItem(也使用自定義視圖)和一個自定義titleView。當navigationItem.titleView和rightBarButtonItem佔用大部分UINavigationBar時,如何停止leftBarButtonItem消失?

leftBarButtonItem小於一定的寬度時,一切都可以正常工作。 rightBarButtonItem可能很大,titleView將調整大小並截斷以適應可用空間,這正是它應該如何以及我想要的。

然而,利用反轉相同的尺寸(大於一定寬度rightBarButtonItem較小和leftBarButtonItem巨大),則完全leftBarButtonItem消失,並且titleView伸展以適應先前會保持兩者leftBarButtonItemtitleView的整個空間。

我試着交換自定義視圖來查看它是否是我做過的事情,但同樣的問題仍然與視圖交換,僅在leftBarButtonItem。我也嘗試使用一個普通的標題,沒有自定義titleView,無濟於事。

在文檔UINavigationItemleftBarButtonItems下注意到「如果沒有足夠的空間顯示數組中的所有項目,那些將與標題視圖(如果存在)或右側按鈕重疊的空間酒吧的一面不顯示。「但是,應該有足夠的空間來容納按鈕和標題,如具有巨大的rightBarButtonItem和截斷標題所示。更何況,只有一個leftBarButtonItem,而不是多個,我需要它來代替後退按鈕功能。

關於如何阻止這種情況發生的任何想法?以下是我正在使用的代碼片段:

self.navigationItem.titleView = [[TitleView alloc] initWithFrame:CGRectMake(0, 0, 220, 44)]; 
self.navigationItem.titleView.backgroundColor=[UIColor clearColor]; 

FollowButton *follow = [[FollowButton alloc] initWithFrame:CGRectMake(0, 0, 62, 34)]; 
UIBarButtonItem *followButton = [[UIBarButtonItem alloc] initWithCustomView:follow]; 
self.navigationItem.rightBarButtonItem = followButton; 

BackButton *back = [[BackButton alloc] initWithFrame:CGRectMake(0, 0, 108, 34)]; 
[back addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:back]; 
self.navigationItem.leftBarButtonItem = backButton; 
+0

看起來你的TitleView太大了。只需進入代碼,看看大屏幕是什麼,你的問題將得到回答。 \t [UIScreen mainScreen] bounds] .size.width – Brian

+0

儘管如此,如果我離開leftBarButtonItem的一個相當小的尺寸,並且使右邊的BarButtonItem變大,那麼它就會填充大部分的導航條, titleView會自動調整大小,文本會被截斷(如預期的那樣)以適應剩餘的小區域。 如果我做的是相反的,rightBarButtonItem很小,leftBarButtonItem很大,leftBarButtonItem被隱藏起來。我希望titleView可以調整大小以適應它們。 – evandinsmore

回答

0

你可以檢查標題欄按鈕的寬度嗎?它是否實際縮小或x位置是否改變,它會被推離屏幕......如果寬度實際上變爲「0」,則應該能夠在加載後將其設置回所需的寬度其他按鈕完成。

+0

我希望它縮小,這將更容易解決。不,尺寸保持不變,隱藏起來。希望的行爲是爲了縮小titleView,就像切換左右按鈕時一樣。 – evandinsmore

+0

但是,你可以通過編程的方式減少物體的寬度,迫使它消失嗎? –

相關問題