Dynamically created UIBarButtonItem doesn't trigger on tap - ios

I've got a UIPickerView that has a UIToolbar attached to it on the parent view's load with this code:
- (void)viewDidLoad {
[super viewDidLoad];
self.itemSortPicker.hidden = false;
pickerData = #[#"Name",#"Item Level",#"Crafting Level",#"Profit",#"Profit Percentage",#"Sell Price",#"Buy Price",#"Supply",#"Demand",#"Rarity"];
self.itemSortPicker.dataSource = self;
self.itemSortPicker.delegate = self;
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleDefault;
[pickerToolbar sizeToFit];
[pickerToolbar setFrame:CGRectMake(0, -pickerToolbar.bounds.size.height, 320, 44)];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(pickerCancel)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
CGRect pickerRect = self.itemSortPicker.bounds;
self.itemSortPicker.bounds = pickerRect;
self.itemSortPicker.frame = CGRectMake(0, 44, 320, 216);
[self.itemSortPicker addSubview:pickerToolbar];
}
Now the problem is, when I tap on either the 'done' or 'cancel' buttons, the associated method doesn't trigger. The method doesn't take any parameters and just does an NSLog action. The PickerView isn't taking focus from the bar because when I tap and drag on the buttons, the PickerView doesn't change.

The problem is that the UIToolbar isn't entirely inside the frame of the UIPickerView. As your code is written, although UIToolbar is a subview of UIPickerView, the toolbar is -pickerToolbar.bounds.size.height above the UIPickerView:
[pickerToolbar setFrame:CGRectMake(0, -pickerToolbar.bounds.size.height, 320, 44)];
so you're unable to interact with its buttons because it's hanging off the edge of its superview's frame. For example, if you changed the pickerToolbar frame like so:
[pickerToolbar setFrame:CGRectMake(0, 0, 320, 44)];
you should be able to interact with it.
If you need this specific placement -pickerToolbar.bounds.size.height above the UIPickerView though, I suggest not adding it as a subview to UIPickerView and placing it appropriately as a subview to UIPickerView's superview instead. In general, if you need to interact with a subview it needs to be entirely within its superview's frame.
Edit: In this case, if you're going to instead add the toolbar to the superview, like so:
[self.view addSubview:pickerToolbar];
I suggest trying out this frame to maintain your original placement:
[pickerToolbar setFrame:CGRectMake(self.itemSortPicker.frame.origin.x, self.itemSortPicker.frame.origin.y - pickerToolbar.bounds.size.height, 320, 44)];
(To calculate this new frame, I've added the UIPicker's x and y origin values to the frame's x and y to adjust for the change in the view's coordinate system.)

I'm not sure of wether the problem is because of the button or the toolbar itself nor the pickerview but a better way of doing this is to do it all programmatically and you have to remove the pickerview from the view:
- (void)viewDidLoad {
[super viewDidLoad];
pickerData = #[#"Name",#"Item Level",#"Crafting Level",#"Profit",#"Profit Percentage",#"Sell Price",#"Buy Price",#"Supply",#"Demand",#"Rarity"];
UIPickerView *itemSortPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 162)];
itemSortPicker.dataSource = self;
itemSortPicker.delegate = self;
itemSortPicker.showsSelectionIndicator = YES;
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(pickerCancel)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone)];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[pickerToolbar setBarStyle:UIBarStyleDefault];
[pickerToolbar sizeToFit];
[toolBar setItems:#[cancelBtn,flexSpace,doneBtn]];
BlaBlaTextField.delegate = self;
BlaBlaTextField.inputView = itemSortPicker;
BlaBlaTextField.inputAccessoryView = pickerToolbar;
}
And not to forget your pickerCancel and pickerDone functions, and of course the delegate methods of your itemSortPicker.
I hope that solves your problem.

Related

Add a ToolBar Done Button for Picker

I am trying to add a done button on the top of the picker as follows. But unfortonately, I could not able to see done button.
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done" style:UIBarButtonItemStyleDone
target:self action:#selector(done)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
CGRectMake(0, self.view.frame.size.height-
picker.frame.size.height-250, self.view.frame.size.width, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
doneButton, nil];
[toolBar setItems:toolbarItems];
categoryTF.inputView = picker;
You forgot to add your toolBar into a view. You can do that as follows:
[self.view addSubview:toolBar];
add this line:
categoryTF.inputAccessoryView = toolBar;
Use toolbar and done button for pickerView
CGRect pickerFrame = CGRectMake(0,kSCREEN_HEIGHT-200,kSCREEN_WIDTH,200);
UIPickerView * pickerview = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerview.delegate = self;
pickerview.dataSource = self;
textField.inputView=pickerview;
UIToolbar *myToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 56)];
myToolbar.barStyle=UIBarStyleBlack;
[myToolbar sizeToFit];
myToolbar.backgroundColor=[UIColor whiteColor];
NSMutableArray *barItems=[[NSMutableArray alloc]init];
UIBarButtonItem *btnItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:btnItem];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDoneClicked)];
[barItems addObject:doneBtn];
[myToolbar setItems:barItems animated:YES];
myToolbar.barStyle = UIBarButtonItemStylePlain;
myToolbar.barTintColor = [UIColor colorWithRed:0.94f green:0.94f blue:0.96f alpha:1.0f];
myToolbar.tintColor=[UIColor blackColor];
txtField.inputAccessoryView=myToolbar;
-(void)pickerDoneClicked
{
[txtField resignFirstResponder];
}

The action of UIBarbuttonItem on UIToolBar not called

I am having trouble as the action of UIBarbuttonItem on UIToolBar is not be called.
In the following code, although the doneBtn on toolBar is tapped, the action doneBtnAction: is not be called.
Do you have any idea to fix it?
- (void)viewDidLoad {
UIPickerView *pickerView = [[UIPickerView alloc] init];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, -44, 320, 44)];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneBtnAction:)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolBar.items = #[flex, doneBtn];
[pickerView addSubview:toolBar];
UITextField *textField = [[UITextField alloc] init];
textField.inputView = pickerView;
}
- (void)doneBtnAction:(UIBarButtonItem *)sender {
NSLog(#"%#", sender);
}
Don't add the toolbar as a subview of the picker view, especially with a negative y origin (No touches reach the toolbar because the taps are clipped to the picker view's frame).
Instead, make the toolbar the inputAccessoryView of the text field.
textField.inputAccessoryView = toolBar;
Complete code:
- (void)viewDidLoad {
UIPickerView *pickerView = [[UIPickerView alloc] init];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneBtnAction:)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolBar.items = #[flex, doneBtn];
UITextField *textField = [[UITextField alloc] init];
textField.inputView = pickerView;
textField.inputAccessoryView = toolBar;
}
One other note - Why not use the standard system Done type for the bar button item?

How to make a UIBarButtonItem selectable on a UIToolbar part of a UIPickerView

I have a two column UIPickerView in a project that I am working on, and I successfully created the UIPickerView, UIToolBar, and UIToolBarItem programmatically. However the toolbar item isn't responding to touch inputs. I have tried on the device and in the Simulator, and the selector method is never being called / reached.
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 216)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *btnAddCredit = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addCreditToUser:)];
NSLog(#"btnAddCredit %hhd",btnAddCredit.isEnabled);
[toolBar setItems:[NSArray arrayWithObjects:flex,btnAddCredit,nil]];
toolBar.userInteractionEnabled = true;
[_pickerView addSubview:toolBar];
_pickerView.delegate = self;
_pickerView.showsSelectionIndicator = YES;
[self.parentViewController.view addSubview:_pickerView];
Here's a quick animated GIF explaining what is happening,

UIPickerView is not aligned properly

I am using a UIPickerView in iOS8 and am having a very strange problem. This is my general implementation:
_providerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 344, self.view.bounds.size.width, 44)];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheet:)];
_providerToolbar.items = #[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], done];
[self.view addSubview:_providerToolbar];
_providerPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 300, 0, 0)];
_providerPickerView.showsSelectionIndicator = YES;
_providerPickerView.dataSource = self;
_providerPickerView.delegate = self;
[self.view addSubview:_providerPickerView];
This seemed to work perfectly fine in iOS7 but in iOS8, the done button seems to be aligned incorrectly. Why is there no space between the end of the done button and the toolbar border?

putting labels, buttons on the navigation bar iOS

I have created custom navigation controller,
I want to be added, a date at the left, a back button on the right and the title next to back button.
I tried to add one label, but it does not work. Please show me a way
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.text = #"My Text";
[self.navigationController.navigationBar addSubview:navLabel];
[self.view addSubview:naviBarObj];
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,8,200,30)];
navLabel.text = #"My Text";
navLabel.textColor = [UIColor redColor];
[naviBarObj addSubview:navLabel];
[navLabel setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:naviBarObj];
try this it will work .It works for me :)
add custom view in UIToolbar
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f,190.0f, 44.01f)];
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] init];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width =10.0f;
[buttons addObject:bi];
[buttons addObject:add your view];
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width =10.0f;
[buttons addObject:bi];
[buttons addObject:add your view];
[tools setItems:buttons animated:NO];
UIBarButtonItem *buttons_ = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = buttons_; //same to leftBarButtonItem
You can also achieve this from Interface Builder
1. First add a UIView which will look like following
2. Then add a UILabel inside the UIView which will look like following
3. In the Center It will look like following
Now add necessary constraint to meet your requirement.
You need to add navigation item to the navigation bar. One per each navigation level. So you should have at least one of them.
** update **
UINavigationItem * myNavItem = [[UINavigationItem alloc] initWithTitle:#"Some title"];
/*
* add buttons and labels
*/
[self.navigationController.navigationBar pushNavigationItem:myNavItem animated:NO];
You need to change the titleView of the topItem. Like this:
naviBarObj.topItem.titleView = navLabel;
** update: Complete code (tested and working) **
UINavigationBar *naviBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(.0, .0, 320.0, 44.0)];
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem.titleView = titleLbl;
[naviBar pushNavigationItem: navItem animated: NO];
[navItem release]; // you don't need this for ARC
[self.view addSubview: naviBar];
[naviBar release]; // you don't need this for ARC

Resources