TabBarItem的圖片默認顏色 讓我甚是頭痛,美工MM說灰色的好醜 T_Tios
1.iOS7如下app
查閱了相關文檔,這樣一個方法字體
/* These methods are now deprecated. Please use -initWithTitle:image:selectedImage:. */ - (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage NS_DEPRECATED_IOS(5_0,7_0,"Use initWithTitle:image:selectedImage: or the image and selectedImage properties along with UIImageRenderingModeAlwaysOriginal");
不過你也看到了,這iOS7開始就棄用了,但至少還能用,若是不介意那煩人的警告的話;ui
2.iOS7以上this
那警告實在鬧心,根據警告內容,我又查了資料,結果比較有趣spa
//UITabBar設置未選中圖片 [yourTabBarItem setImage:[[UIImage imageNamed:@"more.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]]; //UITabBar設置選中圖片 [yourTabBarItem setSelectedImage:[[UIImage imageNamed:@"more.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
有一個共同點就是圖片多調用了code
// Create a version of this image with the specified rendering mode. By default, images have a rendering mode of UIImageRenderingModeAutomatic. - (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode NS_AVAILABLE_IOS(7_0);
也就是設置了UIImageRenderingMode,下面是枚舉orm
它使用UIImageRenderingMode枚舉值來設置圖片的renderingMode屬性。該枚舉中包含下列值: UIImageRenderingModeAutomatic // 根據圖片的使用環境和所處的繪圖上下文自動調整渲染模式。 UIImageRenderingModeAlwaysOriginal // 始終繪製圖片原始狀態,不使用Tint Color。 UIImageRenderingModeAlwaysTemplate // 始終根據Tint Color繪製圖片,忽略圖片的顏色信息。
3.以上只就解決了圖片顏色問題還有文字 顏色仍是默認的顏色圖片
//設置未選中字體顏色 [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:UIColorFromRGB(0x003500), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal]; //設置選中字體顏色 [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: UIColorFromRGB(0xff3500), NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
附上一個顏色宏ci
/** Create a UIColor from a hex value. For example, `UIColorFromRGB(0xFF0000)` creates a `UIColor` object representing the color red. */ #define UIColorFromRGB(rgbValue) \ [UIColor \ colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \ green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \ blue:((float)(rgbValue & 0x0000FF))/255.0 \ alpha:1.0]
參考:http://stackoverflow.com/questions/19563193/uitabbaritem-setfinishedselectedimage-deprecated-in-ios7