Bar button item event not activate in ios 6 or late - uibarbuttonitem

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!

Related

Enters the method but does not display anything

I am trying to call a method to ViewController class form AppDelegate, it enter to the method but the problem is that is not displaying anything , I was looking for, but I can´t find the answer. Some one could help me? thanks
this is the code :
AppDelegate.m
- (void)applicationWillEnterForeground:(UIApplication *)application
{
if (mainDelegate.isflagON=TRUE)
{
ViewController *vc=[ViewController new];
[vc pause];
}
}
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *mainDelegate = [[UIApplication sharedApplication] delegate];
if (mainDelegate.isflagON==false)
{
mainDelegate.isflagON=true;
CGSize viewSize = self.view.bounds.size;
// Add this line
GameScene *scene = [[GameScene alloc] initWithSize:viewSize];
scene.scaleMode = SKSceneScaleModeAspectFill;
self.scene = scene;
// Configure the view.
skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Present the scene.
[skView presentScene:scene];
}else
{
[self doPause];
}
}
-(void) imprime
{
[self doPause];
}
- (void)doPause {
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 200.0f, 200.0f);
backImageView = [[UIImageView alloc] initWithFrame:myImageRect];
[backImageView setImage:[UIImage imageNamed:#"dialogBox.png"]];
[self.view addSubview:backImageView];
[self.view sendSubviewToBack:backImageView];
//set the position of the button
buttonContinuar = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonContinuar setBackgroundImage:[UIImage imageNamed:#"dialogButton.png"]
forState:UIControlStateNormal];
buttonContinuar.frame = CGRectMake(40.0f, 50.0f, 120, 35);
[buttonContinuar addTarget:self action:#selector(buttonWasClicked:) forControlEvents:UIControlEventTouchUpInside];
[buttonContinuar setTitle:#"Continuar" forState:UIControlStateNormal];
//add the button to the view
[self.view addSubview:buttonContinuar];
buttonMapa = [UIButton buttonWithType:UIButtonTypeCustom];
//[button setTitle:#"Continuar" forState:UIControlStateNormal];
[buttonMapa setBackgroundImage:[UIImage imageNamed:#"dialogButton.png"]
forState:UIControlStateNormal];
buttonMapa.frame = CGRectMake(40.0f, 85.0f, 120, 35);
[buttonMapa addTarget:self action:#selector(buttonMapaClicked:) forControlEvents:UIControlEventTouchUpInside];
[buttonMapa setTitle:#"Mapa" forState:UIControlStateNormal];
//add the button to the view
[self.view addSubview:buttonMapa];
buttonMenu = [UIButton buttonWithType:UIButtonTypeCustom];
//[button setTitle:#"Continuar" forState:UIControlStateNormal];
[buttonMenu setBackgroundImage:[UIImage imageNamed:#"dialogButton.png"]
forState:UIControlStateNormal];
buttonMenu.frame = CGRectMake(40.0f, 120.0f, 120, 35);
[buttonMenu addTarget:self action:#selector(buttonMenuClicked:) forControlEvents:UIControlEventTouchUpInside];
[buttonMenu setTitle:#"Menu" forState:UIControlStateNormal];
//add the button to the view
[self.view addSubview:buttonMenu];
}
You can try this
- (void)applicationWillEnterForeground:(UIApplication *)application
{
if (mainDelegate.isflagON=TRUE)
{
ViewController *vc=[[ViewController alloc]init];
[vc pause];
}
}

UINavigationController - change back button to image

Ω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.

How can we put two line in UIBarButtonItem in Navigation Bar

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

InputAccessoryView of a UITextField

I need to stick a toolBar with a UITextField inside to a keyboard.
What if I make the whole toolBar (which I think is the textField's super view) the inputAccessoryView of its textField?
I mean like this:
textField.inputAccessoryView = toolBar; // textField is inside the toolBar
I've been trying on this but I have not made it work yet.
Anyone can help?
Or is there another way to achieve what I want?
- (void)viewDidLoad
{
[self createAccessoryView];
[textField setDelegate:self];
[textField setKeyboardType:UIKeyboardTypeDefault];
[textField setInputAccessoryView:fieldAccessoryView];
}
- (void)createAccessoryView
{
CGRect frame = CGRectMake(0.0, self.view.bounds.size.height, self.view.bounds.size.width, 44.0);
fieldAccessoryView = [[UIToolbar alloc] initWithFrame:frame];
fieldAccessoryView.barStyle = UIBarStyleBlackOpaque;
fieldAccessoryView.tag = 200;
[fieldAccessoryView setBarStyle:UIBarStyleBlack];
UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done:)];
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(#"Previous", #""), NSLocalizedString(#"Next", #""), nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl setMomentary:YES];
UIBarButtonItem *segmentButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[fieldAccessoryView setItems:[NSArray arrayWithObjects:segmentButton, spaceButton, doneButton, nil] animated:NO];
[segmentButton release];
[spaceButton release];
[doneButton release];
[segmentedControl release];
}
When are you setting the property? You may be setting it after the view has already loaded.
Try setting it in
- (void) viewDidLoad

UIToolbar not adding to Keyboard

I am trying to add a UIToolbar to the UIKeyboard so that the username can switch easily between two text fields. However I cannot get it to work. Please can you tell me what I am doing wrong. I have set up the NotificationCenter in my viewDidLoad.
- (void)keyboardWillShow:(NSNotification *)notification {
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
// Now iterating over each subview of the available windows
for (UIView *keyboard in [keyboardWindow subviews]) {
// Check to see if the description of the view we have referenced is UIKeyboard.
// If so then we found the keyboard view that we were looking for.
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
NSValue *v = [[notification userInfo] valueForKey:UIKeyboardBoundsUserInfoKey];
CGRect kbBounds = [v CGRectValue];
UIToolbar *input = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kbBounds.size.width, 50)];
UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithTitle:#"Previous" style:UIBarButtonItemStylePlain target:self action:#selector(previousTextField:)];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(nextTextField:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(hideKeyboard:)];
[input setItems:[NSArray arrayWithObjects:previous,next,space,done,nil]];
if(input == nil) {
UIToolbar *input = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kbBounds.size.width, 50)];
UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithTitle:#"Previous" style:UIBarButtonItemStylePlain target:self action:#selector(previousTextField:)];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(nextTextField:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(hideKeyboard:)];
[input setItems:[NSArray arrayWithObjects:previous,next,space,done,nil]];
}
[input removeFromSuperview];
input.frame = CGRectMake(0, 0, kbBounds.size.width, 30);
[keyboard addSubview:input];
keyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y, kbBounds.size.width, kbBounds.size.height + 60);
for(UIView* subKeyboard in [keyboard subviews]) {
if([[subKeyboard description] hasPrefix:#"<UIKeyboardImpl"] == YES) {
subKeyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y - 30, kbBounds.size.width, kbBounds.size.height);
}
}
}
}
}
}
Create an instance of UIToolbar, Add whichever views you required in toolbar then assign your toolbar instance in inputAccessoryView property of UITextField.
myTextFieldorTextView.inputAccessoryView = boolbar;
Check the below step by step guide to put an toolbar just top of keyboard.
Adding a Toolbar with Next & Previous above UITextField Keyboard

Resources