UIBarButtonItem con imagen personalizada y sin borde


Quiero crear un UIBarButtonItem con una imagen personalizada, pero no quiero el borde que agrega el iPhone, ya que mi Imagen tiene un borde especial.

Es lo mismo que el botón atrás pero un botón adelante.

Esta aplicación es para un proyecto interno, así que no me importa si Apple la rechaza o la aprueba o le gusta: -)

Si utilizo la propiedad initWithCustomView:v del UIBarButtonItem, puedo hacerlo:

UIImage *image = [UIImage imageNamed:@"right.png"];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
[button setBackgroundImage: [[UIImage imageNamed: @"right_clicked.png"] stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateHighlighted];

 button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);

[button addTarget:self action:@selector(AcceptData)    forControlEvents:UIControlEventTouchUpInside];

UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];

[v addSubview:button];

UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithCustomView:v];

self.navigationItem.rightBarButtonItem= forward;

[v release];
[image release];

Esto funciona, pero si tengo que repetir este proceso en 10 vistas, esto no está SECO.

Supongo que tengo que subclase, pero ¿qué ?

  • NSView ?
  • UIBarButtonItem ?

Gracias,

Saludos,

Author: san, 0000-00-00

2 answers

Puede agregar un método a UIBarButtonItem sin subclasificarlo usando la categoría personalizada:

@interface UIBarButtonItem(MyCategory)

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action;

@end

@implementation UIBarButtonItem(MyCategory)

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action{
 // Move your item creation code here
}
@end

Así que en cualquier lugar de su código puede crear un elemento de barra que llame a este método (siempre que incluya un encabezado con su declaración).

P.d. No necesita usar 'v' UIView ya que puede crear UIBarButtonItem con un botón como vista personalizada directamente.
P. P. S. También necesitas [forward release] en tu código.

 44
Author: Vladimir,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-04-21 08:47:58

Otra solución simple es

  1. Arrastre un UIButton estándar
  2. Establezca el estilo del botón a personalizado y configure su imagen para ese botón
  3. Arrástrelo a la UINavigationBar
  4. Set Selector
 50
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-06 06:35:57