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メソッドの最後に、次のコードを追加します.
上記のコードは、実際にはカスタムナビゲーションバーとあまり差がありません.まず、ボタンに伸縮性のある画像を作成し、背景画像に設定します.テキストのフォーマットを指定しました.
「back」ボタンの中の「back」ボタンは、見た目が異なるため、特別にカスタマイズする必要があります.
customizeApperanceメソッドの最後に、backボタンを特別に扱うコードを追加します.
backボタンには異なるcap inset値が設定されていることに注意してください.また、UIBarButtonItemには、backButtonBackgroundImage専用のプロパティがあります.
コンパイル実行、次の図が表示されます.
iOS 5が発売される前に、標準インタフェースのカスタマイズ設計を実現するのは、開発者にとってそんなに簡単ではありません.開発者がdrawRectを書き換えるのは良い方法ですが、開発者も頭が痛いです.
AD:
UIBarButtonItemのカスタマイズ
imagesを開いてbuttonを見つけますtextured_24.pngとbutton_textured_30.pngの2つのファイルで、ナビゲーションバーのボタンの外観を設定します.
ボタンの幅はテキストに依存するため、ボタン画像をサイズ変更可能に設定する必要があります.
これらのボタンでは、一番左と一番右の5画素も伸縮する必要はありませんので、leftとright cap insetsを5に設定する必要があります.
customizeAppearanceメソッドの最後に、次のコードを追加します.
- //customize the apperance for UIBarButtonItems
- UIImage *button30 = [[UIImageimageNamed:@"button_textured_30"] r
- esizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
- UIImage *button24 = [[UIImageimageNamed:@"button_textured_24"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
- [[UIBarButtonItemappearance] setBackgroundImage:button30forState:UIControlStateNormalbarMetrics:UIBarMetricsDefault];
- [[UIBarButtonItemappearance] setBackgroundImage:button24forState:UIControlStateNormalbarMetrics:UIBarMetricsLandscapePhone];
- [[UIBarButtonItemappearance]setTitleTextAttributes:
- [NSDictionarydictionaryWithObjectsAndKeys:
- [UIColorcolorWithRed:220.0/255.0green:104.0/255.0blue:1.0/255.0alpha:1.0],
- UITextAttributeTextColor,
- [UIColorcolorWithRed:1.0green:1.0blue:1.0alpha:1.0],
- UITextAttributeTextShadowColor,
- [NSValuevalueWithUIOffset:UIOffsetMake(0, 1)],
- UITextAttributeTextShadowOffset,
- [UIFontfontWithName:@"AmericanTypewriter"size:0.0],
- UITextAttributeFont,
- nil]
- forState:UIControlStateNormal];
上記のコードは、実際にはカスタムナビゲーションバーとあまり差がありません.まず、ボタンに伸縮性のある画像を作成し、背景画像に設定します.テキストのフォーマットを指定しました.
「back」ボタンの中の「back」ボタンは、見た目が異なるため、特別にカスタマイズする必要があります.
customizeApperanceメソッドの最後に、backボタンを特別に扱うコードを追加します.
- //customize the appeance for "back" on UIBarButtonItems
- UIImage *buttonBack30 = [[UIImageimageNamed:@"button_back_textured_30"]
- resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
- UIImage *buttonBack24 = [[UIImageimageNamed:@"button_back_textured_24"]
- resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 5)];
- [[UIBarButtonItemappearance]setBackButtonBackgroundImage:buttonBack30forState:
- UIControlStateNormalbarMetrics:UIBarMetricsDefault];
- [[UIBarButtonItemappearance]setBackButtonBackgroundImage:buttonBack24forState:
- UIControlStateNormalbarMetrics:UIBarMetricsLandscapePhone];
backボタンには異なるcap inset値が設定されていることに注意してください.また、UIBarButtonItemには、backButtonBackgroundImage専用のプロパティがあります.
コンパイル実行、次の図が表示されます.