Far*_*ler 37 iphone cocoa-touch uitableview uiviewcontroller uinavigationcontroller
将UIToolBar添加到UITableViewController的最简单方法是什么?我依赖于编辑功能,所以我无法轻松地将UITableViewController更改为UIViewController.
Pey*_*loW 51
没问题,UITableViewController是一个子类UIViewController.事实上,在iPhone OS 3.0中,任何UIViewController (和子类)都可以与a一起工作,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;
}
Run Code Online (Sandbox Code Playgroud)
您也可以使用setToolbarItems:animated:而不是分配给toolbarItems属性,以动态方式添加和删除工具栏项.
MyC*_*nie 40
为了使PeyloW的配方工作,我需要添加以下额外的代码行:
self.navigationController.toolbarHidden = NO;
Run Code Online (Sandbox Code Playgroud)
希望有帮助......
小智 13
- (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];
}
Run Code Online (Sandbox Code Playgroud)
在Swift 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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33206 次 |
| 最近记录: |