Ho bisogno di un UIButtoncon immagine e testo . L'immagine dovrebbe essere in alto e il testo verrà visualizzato sotto l'immagine, entrambi dovrebbero essere cliccabili.
Ho bisogno di un UIButtoncon immagine e testo . L'immagine dovrebbe essere in alto e il testo verrà visualizzato sotto l'immagine, entrambi dovrebbero essere cliccabili.
Risposte:
Vedo risposte molto complicate, tutte usando il codice. Tuttavia, se si utilizza Interface Builder , esiste un modo molto semplice per farlo:


Potresti persino usare lo stesso approccio per codice, senza creare UILabels e UIImages all'interno come altre soluzioni proposte. Sempre semplice!
EDIT: in allegato un piccolo esempio con le 3 cose impostate (titolo, immagine e sfondo) con inserti corretti

Backgroundimmagine invece di Image, altrimenti non vedrai il titolo e nemmeno impostare i valori dell'inserzione per riposizionare il titolo.
Penso che tu stia cercando questa soluzione per il tuo problema:
UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setFrame:CGRectMake(0.f, 0.f, 128.f, 128.f)]; // SET the values for your wishes
[_button setCenter:CGPointMake(128.f, 128.f)]; // SET the values for your wishes
[_button setClipsToBounds:false];
[_button setBackgroundImage:[UIImage imageNamed:@"jquery-mobile-icon.png"] forState:UIControlStateNormal]; // SET the image name for your wishes
[_button setTitle:@"Button" forState:UIControlStateNormal];
[_button.titleLabel setFont:[UIFont systemFontOfSize:24.f]];
[_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // SET the colour for your wishes
[_button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; // SET the colour for your wishes
[_button setTitleEdgeInsets:UIEdgeInsetsMake(0.f, 0.f, -110.f, 0.f)]; // SET the values for your wishes
[_button addTarget:self action:@selector(buttonTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside]; // you can ADD the action to the button as well like
... il resto della personalizzazione del pulsante è tuo dovere ora, e non dimenticare di aggiungere il pulsante alla tua vista.
AGGIORNAMENTO # 1 e AGGIORNAMENTO # 2
oppure, se non hai bisogno di un pulsante dinamico, puoi aggiungere il pulsante alla tua vista in Interface Builder e puoi impostare gli stessi valori anche lì. è abbastanza lo stesso, ma ecco anche questa versione in una semplice immagine.
si può anche vedere il risultato finale in Interface Builder come è nello screenshot.

Xcode-9 e Xcode-10 Apple ha apportato alcune modifiche a Edge Inset ora, è possibile modificarlo in Controllo dimensioni.
Seguire i passaggi seguenti:
Passaggio 1: inserisci il testo e seleziona l'immagine che desideri mostrare:
Passaggio 2: selezionare il controllo pulsante in base alle proprie esigenze, come mostrato nell'immagine seguente:
Passaggio 3: ora vai all'ispettore delle dimensioni e aggiungi valore in base alle tue esigenze:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.imageView.image = [UIImage imageNamed:@"your image name here"];
button.titleLabel.text = @"your text here";
ma il codice seguente mostrerà l'etichetta sopra e l'immagine sullo sfondo
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.background.image = [UIImage imageNamed:@"your image name here"];
button.titleLabel.text = @"your text here";
Non è necessario utilizzare etichetta e pulsante nello stesso controllo poiché UIButton ha proprietà UILabel e UIimageview.
Usa questo codice:
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.imageView.frame=CGRectMake(0.0f, 0.0f, 50.0f, 44.0f);///You can replace it with your own dimensions.
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 35.0f, 50.0f, 44.0f)];///You can replace it with your own dimensions.
[button addSubview:label];
Usa questo codice:
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(0, 10, 200, 52)];
[sampleButton setTitle:@"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:@selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton]
Ho riscontrato lo stesso problema e lo risolvo creando una nuova sottoclasse UIButtone sovrascrivendo il layoutSubviews:metodo come di seguito:
-(void)layoutSubviews {
[super layoutSubviews];
// Center image
CGPoint center = self.imageView.center;
center.x = self.frame.size.width/2;
center.y = self.imageView.frame.size.height/2;
self.imageView.center = center;
//Center text
CGRect newFrame = [self titleLabel].frame;
newFrame.origin.x = 0;
newFrame.origin.y = self.imageView.frame.size.height + 5;
newFrame.size.width = self.frame.size.width;
self.titleLabel.frame = newFrame;
self.titleLabel.textAlignment = UITextAlignmentCenter;
}
Penso che la risposta di Angel García Olloqui sia un'altra buona soluzione, se le metti tutte manualmente con il builder di interfacce, ma manterrò la mia soluzione poiché non devo modificare le inserzioni di contenuto per ciascuno dei miei pulsanti.
Crea UIImageViewe UILabel, e imposta immagine e testo su entrambi .... quindi Posiziona un pulsante personalizzato su imageView ed Etichetta ....
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search.png"]];
imageView.frame = CGRectMake(x, y, imageView.frame.size.width, imageView.frame.size.height);
[self.view addSubview:imageView];
UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y,a,b)];
yourLabel.text = @"raj";
[self.view addSubview:yourLabel];
UIButton * yourBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[yourBtn setFrame:CGRectMake(x, y,c,d)];
[yourBtn addTarget:self action:@selector(@"Your Action") forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourBtn];
È necessario creare una visualizzazione immagine personalizzata per l'immagine e un'etichetta personalizzata per il testo e aggiungerla al pulsante come visualizzazioni secondarie. Questo è tutto.
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
yourButton.backgroundColor = [UIColor greenColor];
yourButton.frame = CGRectMake(140, 40, 175, 30);
[yourButton addTarget:self action:@selector(yourButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourButton];
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, yourButton.frame.size.width, yourButton.frame.size.height/2)];
imageView1.image =[UIImage imageNamed:@"images.jpg"];
[yourButton addSubview:imageView1];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, yourButton.frame.size.height/2, yourButton.frame.size.width, yourButton.frame.size.height/2)];
label.backgroundColor = [UIColor greenColor];
label.textAlignment= UITextAlignmentCenter;
label.text = @"ButtonTitle";
[yourButton addSubview:label];
A scopo di test, utilizzare il yourButtonSelected:metodo
-(void)yourButtonSelected:(id)sender{
NSLog(@"Your Button Selected");
}
Penso che ti sarà utile.
È davvero semplice, basta aggiungere un'immagine allo sfondo del tuo pulsante e dare un testo al nome del pulsante per uicontrolstatenormal. Questo è tutto.
[btn setBackgroundImage:[UIImage imageNamed:@"img.png"] forState:UIControlStateNormal];
[btn setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
[btn setTitle:@"Click Me" forState:UIControlStateNormal];