如何在ios5中自定义UIBarButtonItem [英] how to customize UIBarButtonItem in ios5

查看:107
本文介绍了如何在ios5中自定义UIBarButtonItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义UIBarButtonItem,这是我的代码。

I want customize UIBarButtonItem,here is my code.

在ios4.3中它可以工作,但是ios5,这很奇怪。
uibarbuttonitem消失在视线之外,但你点击它仍然有效果。由于是透明的。

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。

我需要帮助,如何制作uibarbuttonitem显示.thanks

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;


推荐答案

iOS中有一个关于新自定义功能的精彩教程5在这里: http://www.raywenderlich.com/4344/ user-interface-customization-in-ios-5

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

这是来自该网站的代码副本,用于处理UIBarButtonItems的自定义,其相当不言自明:

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];

这篇关于如何在ios5中自定义UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆