Internship Summary--uibarbuttonitem and Uinavigationbar's pit

Source: Internet
Author: User

This week has done a lot of UI things, the pit is mainly concentrated in Uibarbuttonitem and Uinavigationbar, record for reference.

Uibarbuttonitem a custom location

It is not possible to select a location directly from the system by default, even if the default position of the button is not clear. If you want to customize the position of the button, there are two options.

First, directly modify the system generated button.

You can modify the position of the image by Uibarbuttonitem the Imageinsets property, thereby indirectly modifying the position of the button. The code is as follows:

UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:image imageWithRenderingMode:mode style:UIBarButtonItemStylePlain target:self action:xxx];rightItem.imageInsets = UIEdgeInsetsMake(-4000);self.navigationItem.rightBarButtonItem = rightItem;

To highlight the key, the first line of code that generates Rightitem does some simplification, without much attention. The Uiedgeinsetsmake method specifies the volume of the image in the upper, left, lower and right four directions respectively.

Second, completely customize the button.

The full customization of the button can be achieved through the CustomView property of the Uibarbuttonitem. You don't write the code specifically.

Comparison of the two methods:
Code simplicity: Obviously the first way is simpler and clearer, and if you just want to move the position simply, the first way is recommended.

UI Aesthetics: Because the first method uses the system default Uibarbuttonitem, it is rendered transparent in the highlighted state. The second method requires a custom UIButton, which is dimmed by default highlighting. Personally think the first way is more beautiful. Of course, the same effect can be achieved by customizing the UIButton, but the code is more complex.

Animation effect: I encountered a requirement in the project, and when the main page TableView down, the buttons on the Navigationbar faded away (Alpha from 1 to 0). In this case, because Uibarbuttonitem does not inherit from UIView, it is impossible to implement. The second method must therefore be chosen.

So, unless you have to be highly customizable, try to choose the first implementation of code code as simple as possible.

Change the background color when cell is selected

The TableView cell of the system is dimmed in the selected load. The artwork feels gray too deep, you need to customize the color selected. You can use the Cell.selectedbackgroundview property of the cell to achieve the effect without using a custom UITableViewCell. The code is as follows:

UIColor *cellBackgroundColor = kColorFromRGBA(0000.02);cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];cell.selectedBackgroundView.backgroundColor = cellBackgroundColor;
Navigationbar Modifying colors

iOS7 and later, is through the Navigationbar bartintcolor to modify the color. The Tintcolor property does not seem to be modified.

However, when dealing with Mfmailcomposeviewcontroller, still encountered a bug. This viewcontroller is the system's own viewcontroller for sending feedback messages. But it seems that the code that modifies the Navigationbar color for it has been invalidated. Having consulted a lot of data, we found a workable solution.

Make sure that the code that modifies the Navigationbar color is called before the Mfmailcomposeviewcontroller object is created. You can therefore modify the global Navigationbar color. Finally, in their own class of viewwillappear method, the color can be changed back.

//这是需要创建MFMailComposeViewController的ViewController的.m文件//关键在于创建mailPicker之前先改全局的navigationBar的颜色,记得在viewWillAppear方法中改回来。[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]][[MFMailComposeViewController alloc] init];mailPicker.mailComposeDelegate = self;- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    [[UINavigationBar appearance] setBarTintColor:oldColor];}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Internship Summary--uibarbuttonitem and Uinavigationbar's pit

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.