Sto provando a visualizzare Pulsante con un testo (singola/multi linea) nella parte inferiore dell'immagine & (deve sembrare circolare) sul superiore; il testo dell'immagine & deve essere evidenziato al clic. Come questo:Creazione UIButton (UIButtonTypeCustom) con un'immagine in alto e etichetta in basso con AutoLayout
Ecco il codice in viewDidLoad
:
btArtAndPainting = [UIButton buttonWithType:UIButtonTypeCustom];
btArtAndPainting.backgroundColor = [[UIColor cyanColor] colorWithAlphaComponent:1.00f];
btArtAndPainting.translatesAutoresizingMaskIntoConstraints = NO;
self.vwContainer.translatesAutoresizingMaskIntoConstraints = NO;
[self.vwContainer addSubview:btArtAndPainting];
[btArtAndPainting setImage:[UIImage imageNamed:kIMGCategoryArtAndPaintingNormal] forState:UIControlStateNormal];
[btArtAndPainting setImage:[UIImage imageNamed:kIMGCategoryArtAndPaintingHighlighted] forState:UIControlStateHighlighted];
[btArtAndPainting setTitle:@"Art and Painting" forState:UIControlStateNormal];
[btArtAndPainting setTitle:@"Art and Painting" forState:UIControlStateHighlighted];
btArtAndPainting.tintColor = [UIColor redColor];
btArtAndPainting.titleLabel.font = [UIFont systemFontOfSize:10.00f];
btArtAndPainting.titleLabel.textColor = [UIColor whiteColor];
btArtAndPainting.titleLabel.textAlignment = NSTextAlignmentCenter;
btArtAndPainting.titleLabel.lineBreakMode = NSLineBreakByWordWrapping | NSLineBreakByTruncatingTail;
btArtAndPainting.titleLabel.numberOfLines = 0;
btArtAndPainting.imageEdgeInsets = UIEdgeInsetsMake(0.00f, 8.00f, 20.00f, 8.00f);
btArtAndPainting.titleEdgeInsets = UIEdgeInsetsMake(32.00f, 0.00f, 0.00f, 0.00f);
NSArray *constraintsWidth = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[btArtAndPainting(60)]|"
options:0
metrics:nil
views:viewDictionary];
NSArray *constraintsHeight = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[searchBar]-10-[btArtAndPainting(60)]"
options:0
metrics:nil
views:viewDictionary];
[self.vwContainer addConstraints:constraintsHeight];
[self.vwContainer addConstraints:constraintsWidth];
[btArtAndPainting layoutIfNeeded];
[btArtAndPainting needsUpdateConstraints];
Il problema è che il testo non viene visualizzato a tutti (in entrambi i normali & stati evidenziati). Guarda le immagini collegate qui sotto. Come utilizzare UIEdgeInset per creare spazio per il testo? Ho anche provato a creare una categoria UIButton
ma l'effetto è più o meno lo stesso.
Se si modifica un ** ** UIButton, Io vi consiglio di creare ** ** CustomView –
@grhnkdlk penso che non sarà richiesto. Potresti dirmi qual è il vantaggio di questo nel mio caso d'uso? – Smarto
In ** UIButton ** è difficile da modificare, ma customviews è molto più flessibile e gratuito. Per me la tua soluzione è * customview * –