The default navigation bar Uinavigationcontroller's caption is text, and the Navigationitem.titleview property makes it easy to replace it with a custom UIView view.
1, using the picture as the title
1
2
|
let
image =
UIImage
(named:
"logo"
)
self
.navigationItem.titleView =
UIImageView
(image: image)
|
2, using the button as the caption
Import Uikit
Class Viewcontroller:uiviewcontroller {
Override Func Viewdidload () {
Super.viewdidload ()
Let button = UIButton (frame:cgrectmake (0, 0, 110, 110))
Button.setimage (UIImage (named: "Btn3"), Forstate:. Normal)
Button.addtarget (Self,action:selector ("titletapped"), forControlEvents:. Touchupinside)
Self.navigationItem.titleView = button
}
Func titletapped () {
Print ("tapped")
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}
3, use the segmented selection control as the caption
Import Uikit
Class Viewcontroller:uiviewcontroller {
Override Func Viewdidload () {
Super.viewdidload ()
Let items=["Phone", "SMS"] as [Anyobject]
Let Segmented=uisegmentedcontrol (Items:items)
Segmented.center=self.view.center
Segmented.selectedsegmentindex=0///default selected item 1th
Segmented.addtarget (Self, Action: "Segmentdidchange:",
forControlEvents:UIControlEvents.ValueChanged)//Add Value change monitor
Self.navigationItem.titleView = segmented
}
Func Segmentdidchange (Segmented:uisegmentedcontrol) {
Get an index of an option
Print (Segmented.selectedsegmentindex)
Get the selected text
Print (Segmented.titleforsegmentatindex (segmented.selectedsegmentindex))
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}