1

我真的很难以UIToolBar编程方式创建一个(没有 xib 文件)。这就是我想要创造的。

但直到现在,我觉得我还没有以正确的方式接近它。我尝试向其添加 barbuttonitem,但无法创建 png 中显示的效果。我该怎么办。需要帮助。

编辑:我添加了更大的图像

4

6 回答 6

3
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:@"toolbar title" style:UIBarButtonItemStylePlain    target:self     action:@selector(onToolbarTapped:)];
NSArray *toolbarItems = [NSArray arrayWithObjects:spaceItem, customItem, spaceItem, nil];

[self setToolbarItems:toolbarItems animated:NO];
于 2012-05-14T06:48:16.457 回答
2

使用 CGRECT 创建您的工具栏,将其放在您想要的位置

UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(x, y, width, height)];

您可以使用背景图像、标题、文本颜色、背景颜色等自定义它。

接下来创建您的按钮

UIBarButtonItem *theButton = [UIBarButtonItem alloc] initWithTitle:@"button title" style:UIBarButtonItemStylePlain    target:self     action:@selector(selector:)];

将其添加到您的工具栏:

NSArray *buttons = [NSArray arrayWithObjects: theButton, nil];
[myToolbar setItems:buttons animated:NO]

将工具栏添加到当前视图

[self.view addSubview:mytoolbar]

刚打字,没测试,希望对你有帮助

于 2012-05-14T06:56:38.817 回答
1

工具栏和按钮具有自定义图像

- (void)viewDidLoad {
    [super viewDidLoad];

    UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, 320, 44)];
    [myToolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolbar_40.png"]] autorelease] atIndex:0];

    UIBarButtonItem *barButtonDone = [self createImageButtonItemWithNoTitle:@"button_down.png" target:self action:@selector(closeMe:)];
    UIBarButtonItem *barButtonShare = [self createImageButtonItemWithNoTitle:@"button_share.png" target:self action:@selector(actionShareButton:)];
    UIBarButtonItem *barButtonFlexibleGap = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]autorelease];
    myToolbar.items = [NSArray arrayWithObjects:barButtonDone,barButtonFlexibleGap,barButtonShare,nil];

    [self.view addSubview:myToolbar];
    [myToolbar release];
}

-(void)closeMe:(id)sender
{
    NSLog(@"closeMe");
}

-(void)actionShareButton:(id)sender
{   
    NSLog(@"actionShareButton");
}

-(UIBarButtonItem *)createImageButtonItemWithNoTitle:(NSString *)imagePath target:(id)tgt action:(SEL)a
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    UIImage *buttonImage = [[UIImage imageNamed:@"button_slice.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
    UIImage *buttonPressedImage = [[UIImage imageNamed:@"button_slice_over.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];

    CGRect buttonFrame = [button frame];
    buttonFrame.size.width = 32;
    buttonFrame.size.height = 32;
    [button setFrame:buttonFrame];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
    imageView.image = [UIImage imageNamed:imagePath];
    [button addSubview:imageView];
    [imageView release];

    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];

    [button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return [buttonItem autorelease];

}

这些png文件需要添加

  1. toolbar_40.png 用于工具栏图像
  2. button_down.png 用于按钮图像
  3. button_share.png 用于按钮图像
  4. button_slice.png 用于按钮背景
  5. button_slice_over.png 用于按钮背景(按下)
于 2012-05-11T06:38:39.127 回答
0

似乎更适合导航栏(至少如果您保持您正在做的事情的精神:IE 在屏幕底部添加标题)

无论如何,我创建了一个简单的测试项目,这就是我为获得图片功能所做的工作,您可能必须以不同的方式将其插入到视图/控制器中,但它相对容易获得您想要的东西并且不使用按钮栏

- (void)viewDidLoad {

    [super viewDidLoad];

    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, 320, 44)];
    [navBar setBarStyle:UIBarStyleBlack];

    UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:@"Your Title"];
    NSArray *array = [[NSArray alloc] initWithObjects:title, nil];

    [navBar setItems:array];

    [self.view addSubview:navBar];
}
于 2012-05-14T16:07:29.260 回答
0
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, 320, 44)];
myToolbar.barStyle = UIBarStyleBlack;

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
myLabel.text = @"Sperre aufheben";
myLabel.textColor = [UIColor whiteColor];
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.backgroundColor = [UIColor clearColor];

[myToolbar  addSubview:myLabel];
[self.view addSubview:myToolbar];

[myLabel release];
[myToolbar release];
于 2012-05-14T04:31:37.830 回答
0
UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackTranslucent;
toolbar.backgroundColor = [UIColor clearColor];

然后添加UIBarButtonItemtoolbar.

于 2012-05-08T14:37:34.800 回答