如何使用Objective-C在工具栏中显示图片

9

我有一个工具栏,代码如下; 我想添加一张名为“toolsIcon.png”的图片,以“工具”显示。

以下是我的代码:

    //BottomBar 

    UIToolbar *toolbar = [[UIToolbar alloc] init];
    toolbar.frame = CGRectMake(0, self.view.frame.size.height - 44,    self.view.frame.size.width, 44);
    [self.view addSubview:toolbar];
    [toolbar release];

    //TOOLBAR ITEMS
    //button 1
    UIBarButtonItem *tools = [[UIBarButtonItem alloc]   initWithTitle:@"Tools" style:UIBarButtonItemStyleBordered target:self    action:@selector(createToolView)];
    [tools setTag:0];



    //add the buttons to the toolbar
    NSArray *buttons = [NSArray arrayWithObjects: tools,nil];
    [toolbar setItems: buttons animated:YES];
    //memory management for buttons - don't waste!
    [tools release];

看起来你没有使用ARC,但你真的应该使用。或者你只是在某个地方找到了这段代码? - marosoaie
停止使用release,它是不再需要的。试试这个链接:https://dev59.com/X2LVa4cB1Zd3GeqPtBrA请回复结果。 - Pulkit Kumar Singh
添加图片的代码在哪里? - Teja Nandamuri
这就是我所询问的。 - user4682708
4个回答

7

您可以创建带图像的UIBarButtonItem并将其添加

UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithImage: yourImage style: UIBarButtonItemStyleBordered target: nil action: nil];

3

由于您在谈论工具栏,我假定您在说的是底部栏,您有两个选择:

  1. 使用UIBarButtonItem,将其图像作为纹理,并删除用户对其的交互。

  2. 创建一个包含png ImageView的UIView,并将其分配为工具栏的子对象。

如果我将来永远不需要该图像作为按钮,则我更喜欢第二种方法。

您需要的代码非常标准...

Objective-C

UIView *customView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 66, 33)];
UIImageView *toolsImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Tools.png"]];

toolsImage.frame = CGRectMake(0, 0, 66, 33);
[customView addSubview: toolsImage];

//And then you add it as a child of your toolbar...

Swift 3

let customView = UIView(frame: CGRect(x: 0, y: 0, width: 66, height: 33))
let toolsImage =  UIImageView(image: UIImage(named: "Tools"))

toolsImage.frame = CGRect(x: 0, y: 0, width: 66, height: 33)
customView.addSubview(toolsImage)

//And then you add it as a child of your toolbar...

我很希望能够对您有所帮助! :)

2

enter image description here

最简单的方法是将一个UIButton放入该项的customView中,就像这样:
UIToolbar * toolbar = [UIToolbar new];
toolbar.frame = CGRectMake(0, h-44, w, 44);
[self.view addSubview:toolbar];

UIButton * button = [UIButton new];
button.frame = CGRectMake(0, 0, 44, 44);
[button setImage:[[UIImage imageNamed:@"tools-128.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
[button setTintColor:[UIColor redColor]];
[button.imageView setContentMode:UIViewContentModeScaleAspectFit];
[button setImageEdgeInsets:UIEdgeInsetsMake(8, 8, 8, 8)];

//good
UIBarButtonItem * toolItem = [[UIBarButtonItem alloc] initWithCustomView:button];

//bad
UIBarButtonItem * itemB = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"tools-48.png"] style:UIBarButtonItemStylePlain target:self action:nil];
[itemB setImageInsets:UIEdgeInsetsMake(8, 8, 8, 8)];

//worse
UIBarButtonItem * itemC = [UIBarButtonItem new];
[itemC setBackgroundImage:[UIImage imageNamed:@"tools-48.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

[toolbar setItems:@[toolItem, itemB, itemC] animated:true];

这可能值得自己制作定制工具栏,这取决于你想要实现什么。

2

您可以使用图像初始化UIBarButtonItem实例,并将其设置为UIToolbar。在UIViewController的viewDidLoad方法中插入以下代码。

UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);

UIBarButtonItem *tool = [[UIBarButtonItem alloc]   initWithImage:[UIImage imageNamed:@"your_image"] style:UIBarButtonItemStylePlain target:self action:@selector(methodCalledOnClick)];
[toolbar setItems: @[tool] animated:YES];
[self.view addSubview:toolbar];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接