簡體   English   中英

如何使用Objective-C在工具欄中顯示圖像

[英]How to display image in toolbar using Objective-C

我有一個工具欄,下面是代碼; 我想添加一個顯示“Tools”的圖像,名為“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];

您可以使用圖像創建UIBarButtonItem並添加它

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

既然你在談論一個工具欄我假設你在談論底欄你有兩個選擇:

  1. 使用UIBarButtonItem將您的圖像作為紋理,並刪除它的用戶交互。

  2. 創建一個UIView,其中包含png的ImageView,並將其指定為工具欄的子項。

如果我將來永遠不需要將該圖像作為按鈕,我更喜歡第二種方法。

你需要的代碼非常標准......

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...

斯威夫特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...

我希望能有所幫助! :)

在此輸入圖像描述

最簡單的方法是將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];

根據您想要實現的目標,滾動您自己的自定義工具欄可能是值得的。

您可以使用圖像初始化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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM