iOSアプリケーション開発初心者チュートリアル:iOS 5 UIKEt新機能(4)


http://mobile.51cto.com/hot-313145_2.htm
一键收藏,随时查看,分享好友!
iOS 5が発売される前に、標準インタフェースのカスタマイズ設計を実現するのは、開発者にとってそんなに簡単ではありません.開発者がdrawRectを書き換えるのは良い方法ですが、開発者も頭が痛いです.
AD:
UIBarButtonItemのカスタマイズ
imagesを開いてbuttonを見つけますtextured_24.pngとbutton_textured_30.pngの2つのファイルで、ナビゲーションバーのボタンの外観を設定します.
ボタンの幅はテキストに依存するため、ボタン画像をサイズ変更可能に設定する必要があります.
これらのボタンでは、一番左と一番右の5画素も伸縮する必要はありませんので、leftとright cap insetsを5に設定する必要があります.
customizeAppearanceメソッドの最後に、次のコードを追加します.

    
    
    
    
  1. //customize the apperance for UIBarButtonItems 
  2. UIImage *button30 = [[UIImageimageNamed:@"button_textured_30"] r 
  3. esizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)]; 
  4. UIImage *button24 = [[UIImageimageNamed:@"button_textured_24"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)]; 
  5. [[UIBarButtonItemappearance] setBackgroundImage:button30forState:UIControlStateNormalbarMetrics:UIBarMetricsDefault]; 
  6. [[UIBarButtonItemappearance] setBackgroundImage:button24forState:UIControlStateNormalbarMetrics:UIBarMetricsLandscapePhone]; 
  7. [[UIBarButtonItemappearance]setTitleTextAttributes: 
  8. [NSDictionarydictionaryWithObjectsAndKeys: 
  9. [UIColorcolorWithRed:220.0/255.0green:104.0/255.0blue:1.0/255.0alpha:1.0], 
  10. UITextAttributeTextColor, 
  11. [UIColorcolorWithRed:1.0green:1.0blue:1.0alpha:1.0], 
  12. UITextAttributeTextShadowColor, 
  13. [NSValuevalueWithUIOffset:UIOffsetMake(0, 1)], 
  14. UITextAttributeTextShadowOffset, 
  15. [UIFontfontWithName:@"AmericanTypewriter"size:0.0], 
  16. UITextAttributeFont, 
  17. nil] 
  18. forState:UIControlStateNormal]; 

上記のコードは、実際にはカスタムナビゲーションバーとあまり差がありません.まず、ボタンに伸縮性のある画像を作成し、背景画像に設定します.テキストのフォーマットを指定しました.
「back」ボタンの中の「back」ボタンは、見た目が異なるため、特別にカスタマイズする必要があります.
customizeApperanceメソッドの最後に、backボタンを特別に扱うコードを追加します.

    
    
    
    
  1. //customize the appeance for "back" on UIBarButtonItems 
  2. UIImage *buttonBack30 = [[UIImageimageNamed:@"button_back_textured_30"
  3. resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)]; 
  4. UIImage *buttonBack24 = [[UIImageimageNamed:@"button_back_textured_24"
  5. resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 5)]; 
  6. [[UIBarButtonItemappearance]setBackButtonBackgroundImage:buttonBack30forState: 
  7. UIControlStateNormalbarMetrics:UIBarMetricsDefault]; 
  8. [[UIBarButtonItemappearance]setBackButtonBackgroundImage:buttonBack24forState: 
  9. UIControlStateNormalbarMetrics:UIBarMetricsLandscapePhone]; 

backボタンには異なるcap inset値が設定されていることに注意してください.また、UIBarButtonItemには、backButtonBackgroundImage専用のプロパティがあります.
コンパイル実行、次の図が表示されます.
iOS应用开发新手教程:iOS5 UIKit新特性(4)_第1张图片