Custom UIBarButton not appearing correctly in UITableView - uibarbuttonitem

I have code that successfully creates a custom UIBarButtonItem that appears on the navigation bar. I use the code in other view controllers, and it works perfectly, but in the UITableViewController, the button appears as the regular BackButtonItem. Can someone help me out? Here is the code that I use in the viewDidLoad method in all of the viewControllers
#implementation P2OListViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.hidesBackButton = YES;
UIImage *backButtonImage = [UIImage imageNamed:#"button-back#2x.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 60, 35);
[backButton addTarget:self
action:#selector(goToMenu:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.backBarButtonItem = back;
- (void)viewWillAppear:(BOOL)animated {
}
- (void)goToMenu:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

hide the default backBarButtonItem by setting its hide property to yes
self.navigationItem.hidesBackButton = YES;
in your viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.hidesBackButton = YES;
UIImage *backButtonImage = [UIImage imageNamed:#"button-back#2x.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 60, 35);
[backButton addTarget:self
action:#selector(goToMenu:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.backBarButtonItem = back;
}
if above not works try this & see what happens:
if (!self.navigationItem.backBarButtonItem.hidden) {
self.navigationItem.backBarButtonItem.hidden = YES;
}
else {
NSLog(#"back button already hidden");
}

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

Bar button item event not activate in ios 6 or late

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!

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

How do I implement UIScrollView programatically

I am launching detail pages from a UITableView and within that detail page there is an image. I would like it to be zoomable, so I'd like the contents of that detail page to sit inside a scrollView. So far no luck, the labels, textviews and images are fine, but I can't get the ScrollView implemented.
here is my .m: THANKS!
- (void)viewDidLoad {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollView.contentSize = CGSizeMake(320, 700);
scrollView.clipsToBounds = YES;
super.view.backgroundColor = [UIColor whiteColor];
[scrollView setUserInteractionEnabled:YES];
self.navigationItem.title = [NSString stringWithFormat:selectedCellItem];
CGRect frame = CGRectMake(0, 0, 10, 10);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.backgroundColor = [UIColor whiteColor];
frame = CGRectMake(0, 320, 10, 10);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
//label.text = selectedCellItem;
label.font=[UIFont fontWithName:#"Verdana-Bold" size:20.0];
label.textAlignment = UITextAlignmentCenter;
[self.view addSubview:label];
[label release];
frame = CGRectMake(0, 320, 320, 60); //Description
UITextView *labelDesc = [[UITextView alloc] initWithFrame:frame];
labelDesc.text = selectedCellDesc;
labelDesc.font=[UIFont fontWithName:#"Verdana" size:12.0];
labelDesc.textAlignment = UITextAlignmentLeft;
//labelDesc.textAlignment = UITextAlignmentTop;
[self.view addSubview:labelDesc];
[labelDesc release];
frame = CGRectMake(0, 0, 320, 320); //Image
UIImageView *imageItem = [[UIImageView alloc] initWithFrame:frame];
[imageItem setImage:[UIImage imageNamed:selectedCellImage]];
// GET THIS ONE WORKING RIGHT>>[imageItem setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.png", [[NSBundle mainBundle] bundlePath], selectedCellImage]]]; //use
//imageItem = [[UIImageView alloc] initWithContentsOfFile:[NSString stringWithFormat:#"shirt.png", [[NSBundle mainBundle] bundlePath]]];
//labelDesc.font=[UIFont fontWithName:#"Zapfino" size:13.0];
//labelDesc.textAlignment = UITextAlignmentCenter;
imageItem.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageItem];
//[imageItem release];
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
I think that you're missing the following line to add the UIScrollView as a subview.
[self.view addSubView:scrollView];

UIControl touchEvent

i have implemented the following viewDidLoad() Method:
- (void)viewDidLoad {
[super viewDidLoad];
// loading images into the queue
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
AFOpenFlowView *openFlow = self.view;
theControl = [[UIControl alloc] initWithFrame:self.view.frame];
NSString *imageName;
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(10, 10, 40, 40);
[btn addTarget:self action:#selector(testMethode) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"<<" forState:UIControlStateNormal];
[btn setHidden:YES];
[theControl addSubview:btn];
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
UIImage *aktuellesImage = imageView.image;
UIImage *scaledImage = [aktuellesImage scaleToSize:CGSizeMake(100.0f, 100.0f)];
[openFlow setImage:scaledImage forIndex:i];
// [(AFOpenFlowView *)self.view setImage:scaledImage forIndex:i];
[imageName release];
[aktuellesImage release];
}
[theControl addSubview:openFlow];
[theControl setUserInteractionEnabled:YES];
[(AFOpenFlowView *)self.view setNumberOfImages:10];
}
And the following touchesEnded() Method:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[btn setHidden:NO];
}
But the touchesEnded doesn't work, and the Button will not shown when i touch the picture, does anybody know what is the Problem???
Greetings Marco
Make sure your view has user interaction enabled in order to register touches. This can be set in your View Controller thusly:
self.view.userInteractionEnabled = YES;

Resources