想定製導航欄嗎?從iOS5開始你就能夠改變導航欄的背景圖片、tintcolor或者標題文本。
這裏咱們將介紹如何在Xcode中定製導航欄。
從這裏你能夠下載到本文用到的按鈕: Buttons.zip
首先須要獲取UINavigationController引用。你能夠在建立UINavigationController時定製導航欄,也能夠在ViewController加載到UINavigationController時定製導航欄。
打開要引用UINavigationController的類,在viewDidLoad方法中定製導航欄的背景圖片,以下列代碼所示:
UIImage
*navbarPortrait
=
[[UIImagep_w_picpathNamed
:@
"navbar_44.png"
]
resizableImageWithCapInsets
:UIEdgeInsetsMake
(
0
,
0
,
0
,
0
)]
;
UIImage
*navbarLandscape
=
[[UIImage p_w_picpathNamed
:@
"navbar_32.png"
]
resizableImageWithCapInsets
:UIEdgeInsetsMake
(
0
,
0
,
0
,
0
)]
;
// Setbackgroundp_w_picpath for all navigationcontrollers
[[UINavigationBar appearance
] setBackgroundImage
:navbarPortrait
forBarMetrics
:UIBarMetricsDefault
]
;
[[UINavigationBarappearance
] setBackgroundImage
:navbarLandscape
forBarMetrics
:UIBarMetricsLandscapePhone
]
;
導航欄的背景圖片必須是下列高度:
豎屏:44px(retina 屏則爲 88px)
橫屏:32px (retina 屏則爲 64px)
BarButton(包括backButton)的背景圖片用backgroundp_w_picpath屬性設置。BarButton的背景圖片必須是下列高度:
豎屏:30px(retina 屏則爲 60px)
橫屏:24px(retina 屏則爲 48px)
若是標題文本過長(超過按鈕圖片的寬度)圖片將被縮放。
// Setbackgroundp_w_picpath for all buttons
UIImage
*buttonPortait
=
[[UIImagep_w_picpathNamed
:
@"button_30.png"
]
resizableImageWithCapInsets
:UIEdgeInsetsMake
(
0,
5,
0,
5
)];
UIImage
*buttonLandscape
=
[[UIImage p_w_picpathNamed
:
@"button_24.png"
]
resizableImageWithCapInsets
:UIEdgeInsetsMake
(
0,
5,
0,
5
)];
[[UIBarButtonItem appearance
] setBackgroundImage
:buttonPortait
forState
:UIControlStateNormal
barMetrics
:UIBarMetricsDefault
];
[[UIBarButtonItemappearance
] setBackgroundImage
:buttonLandscape
forState
:UIControlStateNormal
barMetrics
:UIBarMetricsLandscapePhone
];
// Setbackgroundp_w_picpath for all backbuttons
UIImage
*backButtonPortait
=
[[UIImagep_w_picpathNamed
:
@"back_button_30.png"
]
resizableImageWithCapInsets
:UIEdgeInsetsMake
(
0,
13,
0,
5
)];
UIImage
*backButtonLandscape
=
[[UIImage p_w_picpathNamed
:
@"back_button_24.png"
]
resizableImageWithCapInsets
:UIEdgeInsetsMake
(
0,
10,
0,
4
)];
[[UIBarButtonItem appearance
]setBackButtonBackgroundImage
:backButtonPortait
forState
:UIControlStateNormal
barMetrics
:UIBarMetricsDefault
];
[[UIBarButtonItemappearance
]setBackButtonBackgroundImage
:backButtonLandscape
forState
:UIControlStateNormal
barMetrics
:UIBarMetricsLandscapePhone
];
還能夠經過titleTextAttriutes屬性定製UIBarButton。setTitleTextAttributes:方法用一個NSDictionary參數指定所需選項:
UITextAttributeTextColor,定製文字顏色。
UITextAttributeTextShadowColor,定製文字的陰影色。
UITextAttributeTextShadowOffset,定製陰影的偏移位置。
UITextAttributeFont,定製文本字體。
// Settitletext attributes
NSMutableDictionary
*attributes
=
[[
NSMutableDictionary alloc
] init
];
[attributessetValue
:[UIColor blackColor
] forKey
:UITextAttributeTextColor
];
[attributessetValue
:[UIColor whiteColor
] forKey
:UITextAttributeTextShadowColor
];
[attributessetValue
:[
NSValue valueWithUIOffset
:UIOffsetMake
(
0,
1
)] forKey
:UITextAttributeTextShadowOffset
];
[attributessetValue
:[UIFont fontWithName
:
@"Verdana"size
:
0.0
] forKey
:UITextAttributeFont
];
[[UIBarButtonItemappearance
] setTitleTextAttributes
:attributes forState
:UIControlStateNormal
];
[attributesrelease
];