2014-12-24 21 views
16

Vorrei rendere cliccabile il titolo navigationbar. Quando l'utente fa clic su di esso, dovrebbe eseguire un seguito. Ma non ho idea di come farlo.Rendi mobile il titolo della barra di navigazione

Ho provato quanto segue per ottenere il titolo e applicare il gesto di tocco ad esso.

var subviews = self.navigationController?.navigationBar.subviews 
     if let subviews = subviews { 
      // Better check for array length before accessing to the 1st element 
      var subview = subviews [0] 
     } 

ma la sua darmi errore Variable subview inferred to have type AvyObject, which may be unexpected

risposta

47

Un altro approccio per aggiungere pulsante come titolo del controller di navigazione.

è necessario impostare vista titolo dell'elemento di navigazione al oggetto pulsante

Crea oggetto pulsante in viewDidLoad() metodo:

Swift 3.0 Modifica

let button = UIButton(type: .custom) 
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40) 
button.backgroundColor = UIColor.red 
button.setTitle("Button", for: .normal) 
button.addTarget(self, action: #selector(self.clickOnButton), for: .touchUpInside) 
self.navigationItem.titleView = button 

Qui potete vedere in ultima riga pulsante viene impostato direttamente su titleView di navigationItem che aggiungerà pulsante al centro della barra di navigazione.

metodo d'azione per il pulsante è qui sotto:

func clickOnButton(button: UIButton) { 
} 
+1

Ho provato diversi modi come usare UILabel e toccare gesture ma soprattutto questa soluzione era la più pulita, semplice ed efficiente. – Bigair

+0

Se ti piace lo storyboard: puoi anche aggiungere un IBOutlet e quindi impostare titleView come IBOutlet. self.navigationItem.titleView = titleButton – Pbk

0

per sbarazzarsi di tale errore, specificare un tipo per il vostro if let costante. Ti consigliamo inoltre di modificare il nome costante if let poiché è lo stesso di quello già dichiarato nella riga precedente.

var subviews = self.navigationController?.navigationBar.subviews 
if let subviewArray:NSArray = subviews { 
    // Better check for array length before accessing to the 1st element 
    var subview:UILabel = subviewArray[0] // <-- If the subview's a UILabel 
} 
+0

mi da ancora lo stesso problema. –

+0

@AtifFarrukh lo stesso messaggio di errore? –

+0

sì, lo stesso messaggio –

3

risposta di Kampai aggiornato per Swift 3:

let button = UIButton(type: .custom) 
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40) 
button.setTitle("Button", for: .normal) 
button.addTarget(self, action: #selector(self.clickOnButton), for: .touchUpInside) 
self.navigationItem.titleView = button 
1

posso confermare "la risposta di Kampai aggiornato per Swift 3:" funziona anche per Xcode 9/iOS11:

private func setTouchable(title: String){ 
      let button = UIButton(type: .custom) 
      button.frame = CGRect(x: 0, y: 0, width: 80, height: 40) 
      button.setTitle(title, for: .normal) 
      button.addTarget(self, 
      action: #selector(self.btnAction), 
      for: .touchUpInside) 
      self.navigationItem.titleView = button 
     } 



     @objc func btnAction(sender: Any){ 
      print("Got it!"); 
     } 


.... 
// for example in viewDidLoad: 
      setTouchable(title: "TOUCHME") 
Problemi correlati