我正在尝试动态添加工具栏但不确定如何访问视图控制器以添加工具栏视图

时间:2013-10-15 15:38:55

标签: ios ios5 uitoolbar

我是新手......

我有一个基于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];

1 个答案:

答案 0 :(得分: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 是为了维持创建的两个项目之间的差距。