將UIToolBar添加到UITableViewController的最簡單方法是什麼?我依賴於編輯功能,所以我無法輕鬆地將UITableViewController更改爲UIViewController。將工具欄添加到UITableViewController
回答
完全沒問題,UITableViewController
是UIViewController
的一個子類。而且恰巧在iPhone OS 3.0中,任何UIViewController
(和子類)都可以與UINavigationController
一起使用以提供情境感知工具欄。
爲了這個工作,你必須:
- 確保您使用
UINavigationController
包含需要一個工具欄所有視圖控制器。 - 設置需要工具欄的視圖控制器的
toolbarsItems
屬性。
這幾乎和設置視圖控制器的標題一樣簡單,應該以同樣的方式完成。很可能通過覆蓋initWithNibName:bundle:
初始值設定項。舉個例子:
-(id)initWithNibName:(NSString*)name bundle:(NSBundle*)bundle;
{
self = [super initWithNibName:name bundle:bundle];
if (self) {
self.title = @"My Title";
NSArray* toolbarItems = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addStuff:)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
target:self
action:@selector(searchStuff:)],
nil];
[toolbarItems makeObjectsPerformSelector:@selector(release)];
self.toolbarItems = toolbarItems;
self.navigationController.toolbarHidden = NO;
}
return self;
}
你也可以用setToolbarItems:animated:
,而不是分配給toolbarItems
財產,以動畫方式在飛行中添加和刪除工具欄項目。
爲了使PeyloW的食譜工作,我需要添加以下代碼附加行:
self.navigationController.toolbarHidden = NO;
希望幫助...
同意。我必須將該調用放入viewDidLoad方法中,而不是initWithNibName重寫。然後它很好用。 – 2011-03-16 14:42:18
你剛剛救了我一天,謝謝 – 2011-05-18 14:35:47
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//Initialize the toolbar
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];
//Caclulate the height of the toolbar
CGFloat toolbarHeight = [toolbar frame].size.height;
//Get the bounds of the parent view
CGRect rootViewBounds = self.parentViewController.view.bounds;
//Get the height of the parent view.
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
//Get the width of the parent view,
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
//Create a rectangle for the toolbar
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
//Reposition and resize the receiver
[toolbar setFrame:rectArea];
//Create a button
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]
initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
[[self tableView] reloadData];
}
- (void) info_clicked:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
[toolbar removeFromSuperview];
}
而且在斯威夫特3:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Initialize the toolbar
let toolbar = UIToolbar()
toolbar.barStyle = UIBarStyle.default
//Set the toolbar to fit the width of the app.
toolbar.sizeToFit()
//Caclulate the height of the toolbar
let toolbarHeight = toolbar.frame.size.height
//Get the bounds of the parent view
let rootViewBounds = self.parent?.view.bounds
//Get the height of the parent view.
let rootViewHeight = rootViewBounds?.height
//Get the width of the parent view,
let rootViewWidth = rootViewBounds?.width
//Create a rectangle for the toolbar
let rectArea = CGRect(x: 0, y: rootViewHeight! - toolbarHeight, width: rootViewWidth!, height: toolbarHeight)
//Reposition and resize the receiver
toolbar.frame = rectArea
//Create a button
let infoButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(infoClicked))
toolbar.items = [infoButton]
//Add the toolbar as a subview to the navigation controller.
self.navigationController?.view.addSubview(toolbar)
}
func infoClicked() {
//Handle Click Here
}
這對我很好。我無法添加「UINavigationController」,因此手動添加工具欄是唯一的方法。謝謝! – codingFriend1 2012-11-28 11:24:41
不錯。我認爲這應該是被接受的答案。我想**添加一個工具欄到uitableviewcontroller **,而不是啓用uinavigationcontroller。 – soemarko 2014-04-21 11:46:19
- 1. 如何添加一個工具欄到UITableViewController
- 2. 將工具欄添加到部分
- 3. 將操作添加到Eclipse工具欄
- 4. 將此事件添加到工具欄
- 5. 將按鈕添加到工具欄
- 6. 如何將CheckBox添加到工具欄?
- 7. 將工具欄添加到視圖
- 8. 如何將textField添加到工具欄
- 9. 將NSTitlebarAccessoryViewController添加到標題欄/工具欄的左側
- 10. 將導航按鈕添加到統一標題欄/工具欄
- 11. 如何將後退按鈕添加到操作欄/工具欄
- 12. 將信息添加到matplotlib導航工具欄/狀態欄?
- 13. 添加圖像到工具欄
- 14. 如何添加工具欄
- 15. 添加工具欄JSQMessagesViewController
- 16. 自動添加工具欄
- 17. 如何將工具欄添加到Visual Studio加載項?
- 18. iOS:試圖將導航欄添加到Modal UITableViewController
- 19. 添加工具欄項目上默認的工具欄
- 20. 添加工具欄項目或按鈕DetailViewController工具欄的iPad
- 21. 如何在工具欄下添加工具欄 - android
- 22. 我可以將標籤添加到wxPython工具欄中的單個工具嗎?
- 23. Gmail擴展/小工具API - 如何將按鈕添加到撰寫工具欄?
- 24. 如何將後退箭頭添加到工具欄?
- 25. 將按鈕添加到jqGrid頂部工具欄
- 26. Kivy:將工具欄添加到我的應用程序
- 27. 如何將barButtonItem添加到工具欄中
- 28. 將標題添加到半透明工具欄
- 29. 如何將間隔器添加到Eclipse RCP工具欄?
- 30. 在運行時將按鈕添加到Internet Explorer工具欄
是否需要NavigationController?我想將一個ToolBar添加到不屬於NavigationController的TableViewController中。即使只有一個視圖,我是否需要使用NavigationController? – 2011-05-25 04:53:17
@sirjorj是的'UINavigationController'是必需的*免費*工具欄處理。沒有它,你必須管理你自己的'UIToolbar'視圖實例。 – PeyloW 2011-05-25 15:44:57
如果我不想在此工具欄中放置按鈕,相反,我只想在中間放置一個圖像,那我會做什麼改變?謝謝。 – 2012-07-22 05:08:40