2015-04-12 19 views
9

Mam pasek narzędzi z poniższym kodem; Chciałbym dodać obraz, który wyświetla się za pomocą "Tools" Nazwany "toolsIcon.png".Jak wyświetlić obraz na pasku narzędzi za pomocą Objective-C

Poniżej jest mój kod:

//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]; 
+0

Wygląda na to, że nie używasz ARC i naprawdę powinieneś. A może po prostu znalazłeś gdzieś kod? – marosoaie

+0

Przestań używać wydania, nie jest już potrzebna Spróbuj tego. http://stackoverflow.com/questions/9926978/how-to-add-images-to-uitoolbar Powtórz wynik. –

+0

gdzie jest kod, aby dodać obraz? –

Odpowiedz

7

Można tworzyć UIBarButtonItem z obrazem i dodać go

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

Skoro mówimy o pasku Zakładam, że mówimy o dolnym pasku masz dwie możliwości:

  1. Użyj UIBarButtonItem z obrazem jako teksturą i usuń interlinię użytkownika działania dla niego.

  2. Utwórz UIView, który zawiera ImageView dla twojego png i przypisz go jako dziecko paska narzędzi.

Preferuję drugie podejście, jeśli nigdy nie będę potrzebować tego obrazu jako przycisku w przyszłości.

Kod potrzebne jest dość standardowy ...

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

Mam nadzieję, że był pomocny ! :)

2

enter image description here

Najłatwiej jest zrzucić UIButton do CustomView elementu, tak jak poniżej:

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]; 

Może warto toczenia swój własny pasek narzędzi niestandardowych, zależnie od tego co chcesz osiągnąć.

2

Można zainicjować instancję UIBarButtonItem obrazem i ustawić go na pasku UIToolbar. Wstaw poniższy kod w metodzie UIViewController w postaci 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]; 
Powiązane problemy