gpt4 book ai didi

ios - 我正在尝试动态添加工具栏但不确定如何访问 View Controller 以添加工具栏 View

转载 作者:行者123 更新时间:2023-11-29 03:34:17 24 4
gpt4 key购买 nike

我是新手...

我有一个基于 UITableView 的 View Controller .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];

最佳答案

您应该使用 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 的创建是为了在创建的两个项目之间保持间隙。

关于ios - 我正在尝试动态添加工具栏但不确定如何访问 View Controller 以添加工具栏 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19385359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com