2015-04-12 15 views
9

Ho una barra degli strumenti con il seguente codice; Vorrei aggiungere un'immagine che viene visualizzata con "Strumenti" chiamato "toolsIcon.png".Come visualizzare l'immagine nella barra degli strumenti utilizzando Objective-C

Qui di seguito è il mio codice:

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

Sembra che tu non stia utilizzando ARC e davvero dovresti. O hai appena trovato il codice da qualche parte? – marosoaie

+0

Interrompere l'utilizzo della versione non più necessaria Prova questo. http://stackoverflow.com/questions/9926978/how-to-add-images-to-uitoolbar Rispondi come è il risultato. –

+0

dov'è il codice per aggiungere l'immagine? –

risposta

7

È possibile creare UIBarButtonItem con un'immagine e aggiungerla

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

Dal momento che si sta parlando di una barra degli strumenti Presumo che si sta parlando della barra inferiore avete due scelte:

  1. Utilizzare un UIBarButtonItem con l'immagine come texture e rimuovere l'inter utente azione per esso.

  2. Creare un UIView che contiene l'ImageView per il tuo png e assegnarlo come figlio della barra degli strumenti.

Preferisco il secondo approccio se non avrò mai bisogno di quell'immagine come pulsante in futuro.

Il codice necessario è abbastanza standard ...

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

Spero di essere stato utile ! :)

2

enter image description here

Il modo più semplice è quello di cadere un UIButton nella CustomView della voce, in questo modo:

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

Potrebbe essere utile posizionare il proprio barra degli strumenti personalizzata, a seconda di ciò che si voglio raggiungere.

2

È possibile inizializzare l'istanza UIBarButtonItem con un'immagine e impostarla su UIToolbar. Inserire il codice seguente nel metodo viewDidLoad di UIViewController.

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