有关UINavigationBar自定义问题

nntt 2012-03-12 02:00:32
目前往UINavigationBar中加入的UIBarButtonItem在显示的时候,高度最高可义达到NavigationBar的高度,但是设置LeftBarButtonItem或RightBarButtonItem的时候, LeftBarButtonItem的左边及RightBarButtonItem的右边都有空隙,我想LeftBarButtonItem顶到最左边,RightBarButtonItem顶到最右边,如何实现。
...全文
440 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
newlcc 2012-03-15
  • 打赏
  • 举报
回复
@interface CustomNavigationBar : UINavigationBar
{
UIImageView *navigationBarBackgroundImage;
CGFloat backButtonCapWidth;
IBOutlet UINavigationController* navigationController;
}

@property (nonatomic, retain) UIImageView *navigationBarBackgroundImage;
@property (nonatomic, retain) IBOutlet UINavigationController* navigationController;

-(void) setBackgroundWith:(UIImage*)backgroundImage;
-(void) clearBackground;
-(UIButton*) backButtonWith:(UIImage*)backButtonImage highlight:(UIImage*)backButtonHighlightImage leftCapWidth:(CGFloat)capWidth;
-(void) setText:(NSString*)text onBackButton:(UIButton*)backButton;




#import "CustomNavigationBar.h"

#define MAX_BACK_BUTTON_WIDTH 160.0

@implementation CustomNavigationBar
@synthesize navigationBarBackgroundImage, navigationController;

// If we have a custom background image, then draw it, othwerwise call super and draw the standard nav bar
- (void)drawRect:(CGRect)rect
{
if (navigationBarBackgroundImage)
[navigationBarBackgroundImage.image drawInRect:rect];
else
[super drawRect:rect];
}

// Save the background image and call setNeedsDisplay to force a redraw
-(void) setBackgroundWith:(UIImage*)backgroundImage
{
self.navigationBarBackgroundImage = [[[UIImageView alloc] initWithFrame:self.frame] autorelease];
navigationBarBackgroundImage.image = backgroundImage;
[self setNeedsDisplay];
}

// clear the background image and call setNeedsDisplay to force a redraw
-(void) clearBackground
{
self.navigationBarBackgroundImage = nil;
[self setNeedsDisplay];
}

// With a custom back button, we have to provide the action. We simply pop the view controller
- (IBAction)back:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}

// Given the prpoer images and cap width, create a variable width back button
-(UIButton*) backButtonWith:(UIImage*)backButtonImage highlight:(UIImage*)backButtonHighlightImage leftCapWidth:(CGFloat)capWidth
{
// store the cap width for use later when we set the text
backButtonCapWidth = capWidth;

// Create stretchable images for the normal and highlighted states
UIImage* buttonImage = [backButtonImage stretchableImageWithLeftCapWidth:backButtonCapWidth topCapHeight:0.0];
UIImage* buttonHighlightImage = [backButtonHighlightImage stretchableImageWithLeftCapWidth:backButtonCapWidth topCapHeight:0.0];

// Create a custom button
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];

// Set the title to use the same font and shadow as the standard back button
button.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
button.titleLabel.textColor = [UIColor whiteColor];
button.titleLabel.shadowOffset = CGSizeMake(0,-1);
button.titleLabel.shadowColor = [UIColor darkGrayColor];

// Set the break mode to truncate at the end like the standard back button
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;

// Inset the title on the left and right
button.titleEdgeInsets = UIEdgeInsetsMake(0, 6.0, 0, 3.0);

// Make the button as high as the passed in image
button.frame = CGRectMake(0, 0, 0, buttonImage.size.height);

// Just like the standard back button, use the title of the previous item as the default back text
[self setText:self.topItem.title onBackButton:button];

// Set the stretchable images as the background for the button
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonHighlightImage forState:UIControlStateHighlighted];
[button setBackgroundImage:buttonHighlightImage forState:UIControlStateSelected];

// Add an action for going back
[button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];

return button;
}

// Set the text on the custom back button
-(void) setText:(NSString*)text onBackButton:(UIButton*)backButton
{
// Measure the width of the text
CGSize textSize = [text sizeWithFont:backButton.titleLabel.font];
// Change the button's frame. The width is either the width of the new text or the max width
backButton.frame = CGRectMake(backButton.frame.origin.x, backButton.frame.origin.y, (textSize.width + (backButtonCapWidth * 1.5)) > MAX_BACK_BUTTON_WIDTH ? MAX_BACK_BUTTON_WIDTH : (textSize.width + (backButtonCapWidth * 1.5)), backButton.frame.size.height);

// Set the text on the button
[backButton setTitle:text forState:UIControlStateNormal];
}

- (void)dealloc
{
[navigationBarBackgroundImage release];
[navigationController release];
[super dealloc];
}
咸清 2012-03-15
  • 打赏
  • 举报
回复
UINavigationBar也自定义不就好了

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧