【问题标题】:I'm trying to add a toolbar dynamically but not sure how to get access to the view controller to add tool bar view我正在尝试动态添加工具栏,但不确定如何访问视图控制器以添加工具栏视图
【发布时间】:2013-10-15 15:38:54
【问题描述】:

我是新手...

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

【问题讨论】:

    标签: ios ios5 uitoolbar


    【解决方案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 是为了保持所创建的两个项目之间的间隙而创建的。

    【讨论】:

    • [self.navigationController.toolbar addSubView:myToolBar];解决了背景和位置,但按钮未显示。我想我可以弄清楚。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多