Sunday, December 8, 2013

UINavigationBar custom questions about

Currently UINavigationBar added to the UIBarButtonItem when the display height to a maximum height of NavigationBar achieve justice, but set LeftBarButtonItem or RightBarButtonItem when the left and right of the RightBarButtonItem LeftBarButtonItem have gaps, I think LeftBarButtonItem top to the left, RightBarButtonItem top to the right, how.
Reply:
UINavigationBar also customize not enough
Reply:
@ 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];
}

No comments:

Post a Comment