how to customize UIBarButtonItem in ios5

梦想的初衷 提交于 2019-12-01 21:54:25

问题


I want customize UIBarButtonItem,here is my code.

in ios4.3 it works,but ios5, it‘s very strange. the uibarbuttonitem disappeared out of sight,but you click on it still has the effect。As is transparent。

I need help,how to make the uibarbuttonitem display。thanks

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 50, 30);
[btn setBackgroundImage:[UIImage imageNamed:@"btnRegister.png"] forState:UIControlStateNormal];
[btn setTitle:@"register" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(registerClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = item1;

回答1:


There is a great tutorial about new customization features in iOS 5 here : http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

Here's a copy of code from that site that deals with customization of UIBarButtonItems, its pretty self-explanatory :

UIImage *button30 = [[UIImage imageNamed:@"button_textured_30"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
UIImage *button24 = [[UIImage imageNamed:@"button_textured_24"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];

[[UIBarButtonItem appearance] setBackgroundImage:button30 forState:UIControlStateNormal 
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:button24 forState:UIControlStateNormal 
barMetrics:UIBarMetricsLandscapePhone];

[[UIBarButtonItem appearance] setTitleTextAttributes:
    [NSDictionary dictionaryWithObjectsAndKeys:
    [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0], 
    UITextAttributeTextColor, 
    [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], 
    UITextAttributeTextShadowColor, 
    [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], 
    UITextAttributeTextShadowOffset, 
    [UIFont fontWithName:@"AmericanTypewriter" size:0.0], 
    UITextAttributeFont, 
    nil] 
forState:UIControlStateNormal];



回答2:


My problem is similar to this one. In my case, the button appear no enable. And when you put in it still has the effect. One little solution is change the tint of the button with white but I am looking for a better way :)

My code is:

buttonImage = [UIImage imageNamed:@"top_icon_share.png"];
UIButton *buttonShare = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)];
[buttonShare setBackgroundImage:buttonImage forState:UIControlStateNormal];
[buttonShare addTarget:self action:@selector(buttonSharePressed)
      forControlEvents:UIControlEventTouchUpInside];
[buttonShare setShowsTouchWhenHighlighted:YES];];
    UIBarButtonItem *buttonBarShare = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"top_icon_share.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonSharePressed:)];


来源:https://stackoverflow.com/questions/7950644/how-to-customize-uibarbuttonitem-in-ios5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!