Tmall's iOS Weibo Project Practice (3) Main microblog framework-UIImage prevents automatic rendering after iOS7 _ definition classification,-uiimageios7

Source: Internet
Author: User

Tmall's iOS Weibo Project Practice (3) Main microblog framework-UIImage prevents automatic rendering after iOS7 _ definition classification,-uiimageios7

CAT/CAT sharing, must be excellent

For Original Articles, please reprint them. Reprinted Please note: Yan Nai-yu's blog
Address: http://blog.csdn.net/u013357243

I. Comparison of results

When we set tabBarController's tabBarItem. image, the image turns blue by default, because after ios7, the image is automatically rendered in blue.
Code

UIViewController * home = [[UIViewController alloc] init]; // set the title home. tabBarItem. title = @ "Homepage"; // set the image home when it is not selected. tabBarItem. image = [UIImage imageNamed: @ "tabbar_home"]; // you can specify the home address of the selected image. tabBarItem. selectedImage = [UIImage imageNamed: @ "tabbar_home_selected"]; home. view. backgroundColor = [UIColor blueColor];

Effect:

We need to change it to this effect.

Ii. Solution

There are two solutions: one is to use the built-in function settings of xcode, and the other is to use the pure code method-the custom tabBar that cat and cat have seen before... Weak (a bit more zhuang bi means)

Solution 1: Code Solution

Code:

// Home page UIViewController * home = [[UIViewController alloc] init]; home. tabBarItem. title = @ "Homepage"; home. tabBarItem. image = [UIImage imageNamed: @ "tabbar_home"]; UIImage * selImage = [UIImage imageNamed: @ "tabbar_home_selected"]; // set the rendering mode to the original home. tabBarItem. selectedImage = [selImage imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]; home. view. backgroundColor = [UIColor blueColor];

Note that the original code is compared up and down,

Method 2

Find the third image in the image-render as (Renderer)-original image (original image)

Select the second original image and it will be OK. However, you need to set this method.

3. Create a category for UIImage

Directly create a classification Statement of code.

And then write the code.

Code in UIImage + image. h
/// UIImage + image. h // cat Weibo /// Created by apple on 15-7-22. // Copyright (c) 2015 znycat. all rights reserved. // # import <UIKit/UIKit. h> @ interface UIImage (image)/*** loads the original image, and has not rendered ** @ param imageName image name ** @ return UIImage object */+ (instancetype) imageWithOriginalName :( NSString *) imageName; @ end
Code in UIImage + image. m
/// UIImage + image. m // cat Weibo /// Created by apple on 15-7-22. // Copyright (c) 2015 znycat. all rights reserved. // # import "UIImage + image. h "@ implementation UIImage (image) + (instancetype) imageWithOriginalName :( NSString *) imageName {// automatically rendered after ios7, there are no two ways to render // 1: find the third image in the image -- render as (Renderer) -- original image (original image) // 2: The Code sets UIImage * image = [UIImage imageNamed: imageName]; // set the rendering mode to original return [image imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];} @ end
Iv. Call

In this way, you can simply use a code to complete the call.

First, import the file in the header file.

#import "UIImage+image.h"

And then call it.

    home.tabBarItem.selectedImage = [UIImage imageWithOriginalName:@"tabbar_home_selected"];

Even

Ps: Pay attention to code extraction, which is easier to use. Simply put, if two sentences of code can be written in one sentence, it will be extracted. In the past, the cat and Cat thought that one or two sentences would not matter, just copy it. Some time ago, I tried to help my project suffer a big loss and cry first, % >_<%

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.