ΩI've been trying to do this for like an hour now, nothing seems to work:
Things I've tried:
1) Doesn't work.
UIImage *image = [UIImage imageNamed:#"scr1_button_topbar.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
[v addSubview:button];
UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithCustomView:v];
self.navigationItem.backBarButtonItem= forward;
2) Works, but looks REALLY stupid, it leaves the "Back" text there.
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"scr2_btnback"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
3) Button doesn't show
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"scr2_btnback"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
And setting the text to #""
4) Setting the left bar button item - Works, but that's a little cheating, I want the back button, not the left button.
UIImage *backImage = [UIImage imageNamed:#"scr2_btnback.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
[backButton setImage:backImage forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton] ;
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = backBarButtonItem;
Just got told, you can't edit the view of the backBarButtonItem... YEEY me.
Related
I tried this code:
custom barbuttonitem:
-(id)initWithImage:(UIImage *)image title:(NSString*)title target:(id)target action:(SEL)action {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.titleLabel.font = [UIFont boldSystemFontOfSize:12];
button.titleLabel.shadowOffset = CGSizeMake(0, -1);
button.titleLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.5];
button.titleLabel.textColor = [UIColor redColor];
[button setTitle:title forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
[view addSubview:button];
self = [[UIBarButtonItem alloc] initWithCustomView:view];
return self;
}
and used it:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"my_account_icon.png"] title:#"" target:self action:#selector(buttonClick:)];
But in ios 6 event not activate, ios 5 run nomal, please help me !!!!
UIBarButtonItems with a customView set will not fire actions.
Try the following:
-(id)initWithImage:(UIImage *)image title:(NSString*)title target:(id)target action:(SEL)action
{
//create whatever custom views you need
UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
UITapGestureRecognizer * gestureRec = [[UITapGestureRecognizer alloc] initWithTarget: target action: action];
[view addGestureRecognizer: gestureRec];
//add your custom subviews to the view
self = [[UIBarButtonItem alloc] initWithCustomView:view];
return self;
}
Adding gesture recognizer to the subview of the UIBarButtonItem worked for me, even if UIBarButtonItem is initialized with the custom view.
Good luck!
I have a UIView that I've made into a button that's added below my Table View:
myUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120,50)];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setAutoResizingMask: [UIViewAutoResizingFlexibleWidth|UIViewAutoResizingFlexibleHeight];
[myButton setFrame:CGRectMake(20,0,80,40)];
myButton.titleLabel.textColor = [UIColor blackColor]; //Doesn't seem to work
[myButton setTitle:#"Test" forState:UIControlStateNormal];
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
[myButton setBackgroundImage:[[UIImage imageNamed:#"myImage.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[myUIView addSubView:myButton];
No matter what I try, the font always seems to be white. What can I do to change the font's color?
You want to use setTitleColor: forState:
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
"Now Playing" is in One line in UIBarButtonItem. I have to put it in two lines, like "Now" is ay top and "Playing" is at bottom.I have written the following line of code:-
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Now Playing"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView)];
self.navigationItem.rightBarButtonItem = flipButton;
So i want to pu line break in between "Now Playing". So please help me out.
Yes you can. It is fairly simple to do. Create a multiline button and use that. The "trick" is to set the titleLabel numberOfLines property so that it likes multilines.
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.numberOfLines = 0;
[button setTitle:NSLocalizedString(#"Now\nPlaying", nil) forState:UIControlStateNormal];
[button sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
When you specify a button type of custom it expects you to configure everything... selected state, etc. which is not shown here to keep the answer focused to the problem at hand.
- (void)createCustomRightBarButton_
{
UILabel * addCustomLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 64, 25)] autorelease];
addCustomLabel.text =#"Now\nPlaying";
addCustomLabel.textColor = [UIColor whiteColor];
addCustomLabel.font = [UIFont boldSystemFontOfSize:11];
addCustomLabel.numberOfLines = 2;
addCustomLabel.backgroundColor = [UIColor clearColor];
addCustomLabel.textAlignment = UITextAlignmentCenter;
CGSize size = addCustomLabel.bounds.size;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
[addCustomLabel.layer renderInContext: context];
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage * img = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);
CGContextRelease(context);
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:img
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView)]
autorelease];
}
You can host a UIButton as customView inside your bar button (either set the bar button item customView or you can drag one directly on top of your UIBarButtonItem), hook it up as an outlet, then do;
-(void) viewDidLoad
{
//...
self.customButton.titleLabel.numberOfLines = 2;
self.customButton.suggestionsButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
self.customButton.suggestionsButton.titleLabel.textAlignment = UITextAlignmentCenter;
}
which in my case becomes
I create 2 PNG images by myself, and it looks good.
UIImage *img = [UIImage imageNamed:#"nowplaying.png"];
UIBarButtonItem *nowPlayingButtonItem = [[[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleBordered target:delegate action:#selector(presentNowPlayingMovie)] autorelease];
I have a toolbar created programaticaly with 3 buttons (only 1 shown here). After a lot of Googling I was able to set a nice background image but now I don't know where to put
the code for the action: target:self action:#selector(pressButton3:) method. So I have a non-working button with nice colorful image. I've tried a lot of examples and if the button is working the image is not working and vice versa. Please help.
//Add buttons
UIImage *buttonImage = [UIImage imageNamed:#"mapp.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithCustomView:button];
[systemItem1 initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:#selector(pressButton1:)];
//add to array
NSArray *items = [NSArray arrayWithObjects: systemItem1, nil];
I think you're button should work if you add:
[button addTarget:self action:#selector(pressButton1:) forControlEvents:UIControlEventTouchUpInside];
And remove the line:
[systemItem1 initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:#selector(pressButton1:)];
I want my leftBarButtonItem to be a red button. I haven't figured out how to do that. I'm using the following code to construct the navigation bar and button.
Any help is appreciated.
lq
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navBar.barStyle = UIBarStyleBlack;
navBar.translucent = YES;
[self.view addSubview:navBar];
[navBar release];
UIBarButtonItem *clearButton = [[UIBarButtonItem alloc]
initWithTitle:#"Clear"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(myAction:)];
navItem.leftBarButtonItem = clearButton;
[navBar pushNavigationItem:navItem animated:false];
[navBar setDelegate:self];
[navItem release];
[clearButton release];
Maybe you will find a nicer solution in this posting at Stack Overflow or you can go the way using a UISegmentedControl
UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Clear", nil]] autorelease];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = [UIColor redColor];
...
UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithCustomView:button];
navItem.leftBarButtonItem = clearButton;