您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS5 的 UIAppearance:輕鬆統一 UIBarButtonItem 外觀

編輯:iOS 開發綜合

有時候你並不想用下面這種一行行設定的方式,把整個導覽列按鈕的外觀、字體、背景全部改掉:

UIBarButtonItem *rightItem = [UIBarButtonItem barButtonWithStyle:UIBarButtonStyleRoundedRectangle title:@"Save" action:@selector(saveButtonClicked) delegate:self];

   self.navigationItem.leftBarButtonItem = rightItem;

 

這裡有個方法能做全局或局部的修改:iOS5 提供了一個強大的工具 UIAppearance,可以輕鬆統一你的介面,它提供以下兩個方法:

+ (id)appearance

+ (id)appearanceWhenContainedIn:(Class <>)ContainerClass,...

第一個方法是全局統一修改,例如設定 UINavigationBar 的 tintColor:

[[UINavigationBar appearance] setTintColor:myColor];

第二個方法是只在某個類別出現時才改變,例如:

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], [UIPopoverController class], nil] setTintColor:myPopoverNavBarColor];

 

1. 修改背景:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:leftButton forState:0 barMetrics:UIBarMetricsDefault];

 

2. 修改字體、陰影、字體顏色:

NSDictionary* textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:

    BAR_BUTTON_TITLE_TEXT_COLOR, UITextAttributeTextColor,

    BAR_BUTTON_TITLE_FONT, UITextAttributeFont,

    BAR_BUTTON_TITLE_SHADOW_COLOR, UITextAttributeTextShadowColor,

    [NSValue valueWithCGSize:CGSizeMake(1, 1)], UITextAttributeTextShadowOffset,

    nil];

 

 [[UIBarButtonItem appearance] setTitleTextAttributes:textAttributes forState:0];

 

3. 修改 UIBarButtonItem 中文字的位置:

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(2, -1) forBarMetrics:UIBarMetricsDefault];

 

透過 UIAppearance,你不必逐一設定每個物件,只要設定一次,全 App 的同類元件外觀就能保持一致,大幅降低重複工作。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved