Necesito crear un botón programáticamente con una imagen para el estado normal y resaltado, así como el texto. No puedo construirlo usando Interface Builder, porque necesito crear botones sobre UIScrollView
. Aquí está el código que tengo hasta ahora:Establecer imagen y texto mediante programación en UIButton
- (void)loadView {
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
scrollView.contentSize=CGSizeMake(320,960);
UIImageView *tempImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpeg"]];
UIImage * buttonImage = [UIImage imageNamed:@"contentlist_active.png"];
self.view=scrollView;
[scrollView addSubview:tempImageView2];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(22, 100, 277, 32);
[btn setImage:buttonImage forState:UIControlStateNormal];
[btn setTitle:@"hello world" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[scrollView addSubview:btn];
}
Pero el texto en el botón no se muestra. Si hago un comentario al setImage
para button
, entonces el texto muestra perfectamente, de lo contrario no. ¿Puedo tener texto y una imagen al mismo tiempo?
¿Puede decirme cómo puedo alinear el texto, ya sea a la izquierda o a la derecha, si voy a colocar texto en árabe, por ejemplo. –