[英]UISegmentedControl in the Navigation Bar with the Back button
我正在以編程方式在導航欄中添加一個UISegmentedControl
,其中titleView
應該是。 但正如Apple文檔在titleView
下提到的titleView
, 如果leftBarButtonItem不是nil , 則忽略此屬性 。
但我也想要后退按鈕。 就像他們在自己的圖像中說明的那樣!
下面是我添加UISegmentedControl
的代碼。
self.navigationItem.leftBarButtonItem = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Filter_Personnal", @"Filter_Department", @"Filter_Company", nil];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
self.navigationItem.titleView = statFilter;
還有另一種方法可以添加UISegmentedControl
和Back按鈕嗎?
謝謝。
試試這個
刪除這一行---> self.navigationItem.leftBarButtonItem = nil;
改為添加它
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Filter_Personnal", @"Filter_Department", @"Filter_Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
[statFilter sizeToFit];
self.navigationItem.titleView = statFilter;
只有改變是我添加了這一行:
[statFilter sizeToFit];
希望這可以幫助 !!!
您可以使用自定義視圖創建UIBarButtonItem
,該視圖可能是您的UISegmentedControl
。
以下內容可能有效。
//create segmented control with items
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"One", @"Two", nil]];
//create bar button item with segmented control as custom view
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
//add segmented control bar button item to navigation bar
[[[self navigationController] navigationItem] setRightBarButtonItem:barButtonItem];
我沒有對此進行測試,但它應該沿着您需要的正確線路進行測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.