This is what I am doing to display a UIToolbar in a UITextField's acceccory view.. Unfortunately I am not able to see it for some reason. What am I doing wrong here?
UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneTyping)];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered
target:self action:#selector(gotoNextTextfield:)];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
NSArray *items = [NSArray arrayWithObjects:item2, flexiableItem, item1, nil];
toolbar.items = items;
[inputAccView addSubview:toolbar];
[self.msgTextField setInputAccessoryView:inputAccView];
Update:
if I remove self.msgTextField.delegate = self; then I can see the toolbar.. but why??
Try replacing
[inputAccView addSubview:toolbar];
[self.msgTextField setInputAccessoryView:inputAccView];
with
[self.msgTextField setInputAccessoryView:toolbar];
In your .h file add <UITextFieldDelegate>
Then in your - (void)viewDidLoad try This, it works fine for me:
// Keyboard Tool Bar
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
[toolbar sizeToFit];
UIBarButtonItem *nextField =[[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextField)];
UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(resignKeyboard)];
NSArray *itemsArray = [NSArray arrayWithObjects: nextField, flexButton, doneButton, nil];
[toolbar setItems:itemsArray];
[self.msgTextField setInputAccessoryView:toolbar];
Related
I try to add UINavigationBar programmatically and set bar button items.
I tried:
self.artificialNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.artificialNavBar.backgroundColor = [UIColor whiteColor];
UIBarButtonItem *bbiDone = [[UIBarButtonItem alloc] initWithTitle:#"Готово" style:UIBarButtonItemStyleDone target:nil action:nil];
UIBarButtonItem *bbiTry = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleDone target:nil action:nil];
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem.leftBarButtonItem = bbiDone;
navItem.rightBarButtonItem = bbiTry;
self.artificialNavBar.items = #[ navItem ];
[self.view addSubview:self.artificialNavBar];
However, only left bar button appears, right one is hidden. Did i miss something?
Declare
#property (nonatomic, strong) UINavigationItem *navItem;
in the .h file of this class where you have written all these and then replace your code with below code.
self.artificialNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.artificialNavBar.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.artificialNavBar];
UIBarButtonItem *bbiDone = [[UIBarButtonItem alloc] initWithTitle:#"Готово" style:UIBarButtonItemStyleDone target:nil action:nil];
UIBarButtonItem *bbiTry = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navItem = [[UINavigationItem alloc] init];
[self.navItem setLeftBarButtonItem:bbiDone animated:NO]
[self.navItem setRightBarButtonItem:bbiTry animated:NO]
[self.artificialNavBar setItems:#[self.navItem] animated:NO]
and make sure the navbar is nonatomic and strong.
Below is the code where i get this error.
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, self.view.bounds.size.height - 44.0f, self.view.bounds.size.width, 44.0f)];
toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:toolBar];
UIBarButtonItem *people = [[UIBarButtonItem alloc] initWithTitle:#"people" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *food = [[UIBarButtonItem alloc] initWithTitle:#"food" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *nature = [[UIBarButtonItem alloc] initWithTitle:#"nature" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *sports = [[UIBarButtonItem alloc] initWithTitle:#"sports" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *cats = [[UIBarButtonItem alloc] initWithTitle:#"cats" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UILabel *lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(0,5,100,20)];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor whiteColor];
lbl1.userInteractionEnabled=YES;
[self.view addSubview:lbl1];
lbl1.text= #"TEST";
[toolBar setItems:#[space, lbl1, people, food, nature, sports, cats, space]];
Your mistake is in this line because you add label in toolBar. Use this code
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, self.view.bounds.size.height - 44.0f, self.view.bounds.size.width, 44.0f)];
toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:toolBar];
UIBarButtonItem *people = [[UIBarButtonItem alloc] initWithTitle:#"people" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *food = [[UIBarButtonItem alloc] initWithTitle:#"food" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *nature = [[UIBarButtonItem alloc] initWithTitle:#"nature" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *sports = [[UIBarButtonItem alloc] initWithTitle:#"sports" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *cats = [[UIBarButtonItem alloc] initWithTitle:#"cats" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(0,64,100,20)];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor redColor];
lbl1.userInteractionEnabled=YES;
[self.view addSubview:lbl1];
lbl1.text= #"TEST";
[toolBar setItems:#[space, people, food, nature, sports, cats, space]];
toolBar item clicked event
-(IBAction)tap:(UIBarButtonItem*)sender{
lbl1.text= sender.title;
}
I've created a UIToolBar and set two UIBarButtonItems and then assigned this toolbar to self.navigationItem.rightBarButtonItem.
Everything works perfectly except there is a line on top of my buttons.
The code:
UIBarButtonItem *addItem = [[UIBarButtonItem alloc] initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(addNewRow:)];
UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithTitle:#"Edit"
style:UIBarButtonItemStylePlain
target:self
action:#selector(editRow:)];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[toolBar setItems:#[addItem, editItem]];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolBar];
Please see my pic:
You get rid of the line by getting rid of the toolbar.
UIBarButtonItem *addItem = [[UIBarButtonItem alloc] initWithTitle:#"Add" style:UIBarButtonItemStylePlain target:self action:#selector(addNewRow:)];
UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStylePlain target:self action:#selector(editRow:)];
self.navigationItem.rightBarButtonItems = #[ addItem, editItem ];
I am using a UIToolBar and have added a UIBarButtonItem to it. When I click the button it does nothing. It does not respond to action event. This is my code :
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(x, y, width, toolBarHeight)];
[toolBar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(changeText:)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[self.view addSubview:toolBar];
Try this and see if this helps. Again your code looks fine.
// create an array for the buttons
NSMutableArray* BarbuttonsArray = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(changeText:)];
barButtonDone.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray addObject:barButtonDone];
[toolbar setItems:BarbuttonsArray animated:YES];
I literally copied and pasted your code into a test project of mine and this is what I get. The only change I did was the method name
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolBar setBarStyle:UIBarStyleDefault];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(changeText)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[self.view addSubview:toolBar];
}
-(IBAction) changeText
{
NSLog(#"changeText ...");
}
2014-01-25 13:59:15.882 001-test-proj[15596:a0b] changeText ...
In viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES animated:NO];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Articles" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Source" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Aa" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Rabbit" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
toolBar.items = toolBarItems;
[self.view addSubview:toolBar];
Method:
- (void)backButtonTapped {
[self.navigationController popViewControllerAnimated:YES];
}
I have a breakpoint on the call inside the method but it never gets called. Why is this method never getting called?
I had a UITapGestureRecognizer on the whole view that intercepted the tap on the UIBarButton. I solved it thanks to this answer, which basically stopped the UITapGestureRecognizer from beginning unless it was outside of the UIToolBar.
Your code is not complete and I dont know if you didn't implement it or just didn't wrote it here, so just to make sure, this should work:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Articles" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)]];
toolBar.items = toolBarItems;
[self.view addSubview:toolBar];
Try like this. Its working fine.
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]initWithTitle:#"back"style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
[self.view addSubview:toolbar];