Pergunta

Já vi muitos dos aplicativos que têm um botão de informações (a letra "i" com um círculo em torno dele) na UINavigationBar. Como faço para adicionar este tipo de botão?

Foi útil?

Solução

A resposta anterior estava perto, não chegou a compilar. Aqui está o que você realmente quer:

// Info button
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];

Outras dicas

Há vazamento de memória na resposta acima. Você deve usar "disparo automático" para evitá-lo. Tentei editar a resposta, mas eu não era bem sucedida até agora.

// Info button
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease];

Esta é a maneira correta de fazê-lo.

Você deve ser capaz de adicionar um botão tipo "Info Light" ao navigationItem do controlador de vista que o UINavigationController está a ser apresentado. Algo como isso poderia ir em seu código controlador de vista:

- (void) viewDidLoad {
  UIButton * info = [UIButton buttonWithType:UIButtonTypeInfoLight];
  // set your button's target and selector for whatever action here.
  self.navigationItem.rightBarButtonItem.customView = info;
}

Você também pode usar alvo e seletor de seu atual do item de botão da barra para o seu botão de informações.

UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self.barButtonItem.target action:self.barButtonItem.action forControlEvents:UIControlEventTouchUpInside];
self.barButtonItem.customView = infoButton;

Desta forma, quaisquer ações ou segues acoplados a seu item de botão da barra será transferido para o seu botão de informações.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top