0

我是新手...

我有一个基于 UITableView .h 文件的视图控制器

@interface OAFeaturesViewController : UITableViewController<PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate> 

我可以创建/添加工具栏,但是当我使用它时它会附加到 tableView

[self.view addSubview:myToolBar];

如何获取对主窗口的引用,以便将其添加到它所属的主屏幕底部?
.m 文件

UIToolbar *myToolBar = [[UIToolbar alloc]init];
myToolBar.frame = CGRectMake(0,0,430, 44);
UIBarButtonItem *takePicButton = [[UIBarButtonItem alloc] initWithTitle:@"Take Pic" style:(UIBarButtonItemStyleBordered) target:self action:@selector(takePictureWithCamera)];

UIBarButtonItem *cancelPicButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:(UIBarButtonItemStyleBordered) target:self action:@selector(cancelPictureWithCamera)];


NSArray *items = [NSArray arrayWithObjects: takePicButton, cancelPicButton, nil];
[myToolBar setItems:items animated:NO];

[self.view addSubview:myToolBar];

UIImage *toolbarBkg = [[UIImage imageNamed: @"Navbar_background_solidblue.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0];

if ([myToolBar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)])
{
    [myToolBar setBackgroundImage:toolbarBkg forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    NSLog(@"toolbar responds to selector");
}
else {
    NSLog(@"toolbar does NOT respond to selector");
    UIImageView *background = [[UIImageView alloc] initWithImage:toolbarBkg];
    background.frame = myToolBar.frame;
    [myToolBar insertSubview:background atIndex:0];

}


[self.view  bringSubviewToFront:myToolBar];
4

1 回答 1

1

您应该使用UINavigationController并激活工具栏:

self.navigationController.toolbarHidden=NO;

然后,如果您想将项目添加到工具栏:

UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];

NSArray *items = [NSArray arrayWithObjects:item1, flexibleItem, item2, nil];   
self.toolbarItems = items;

[flexibleItem release];
[item1 release];
[item2 release];

创建flexibleItem是为了保持创建的两个项目之间的间隙。

于 2013-10-15T15:55:16.233 回答