I've created a UIToolBar with UIBarButtonItems but when I tap them, the selector's are not being called. Any help would be greatly appreciated.
UPDATE
Based on suggestions in the comments, I modified my code, but unfortunately, the buttons still do not work.
-(void)buildBottomMenu
{
UIImage *newsImg = [UIImage imageNamed:#"243-globe"];
UIBarButtonItem *newsBtnItem = [[UIBarButtonItem alloc] initWithImage:newsImg style:UIBarButtonItemStylePlain target:self action:#selector(loadHome)];
UIImage *weatherImg = [UIImage imageNamed:#"25-weather"];
UIBarButtonItem *weatherBtnItem = [[UIBarButtonItem alloc] initWithImage:weatherImg style:UIBarButtonItemStylePlain target:self action:#selector(loadWeather)];
UIImage *sportsImg = [UIImage imageNamed:#"63-runner"];
UIBarButtonItem *sportsBtnItem = [[UIBarButtonItem alloc] initWithImage:sportsImg style:UIBarButtonItemStylePlain target:self action:#selector(loadSports)];
UIImage *entrnImg = [UIImage imageNamed:#"178-city"];
UIBarButtonItem *entrnBtnItem = [[UIBarButtonItem alloc] initWithImage:entrnImg style:UIBarButtonItemStylePlain target:self action:#selector(loadEntertainment)];
UIImage *videoImg = [UIImage imageNamed:#"69-display"];
UIBarButtonItem *videoBtnItem = [[UIBarButtonItem alloc] initWithImage:videoImg style:UIBarButtonItemStylePlain target:self action:#selector(loadVideoPage)];
UIImage *moreImg = [UIImage imageNamed:#"259-list"];
UIBarButtonItem *moreBtnItem = [[UIBarButtonItem alloc] initWithImage:moreImg style:UIBarButtonItemStylePlain target:self action:#selector(loadOptionsPage)];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
[_bottomBar setItems:[NSArray arrayWithObjects:flexibleSpace,newsBtnItem,flexibleSpace,
weatherBtnItem,flexibleSpace,
sportsBtnItem,flexibleSpace,
entrnBtnItem,flexibleSpace,
videoBtnItem,flexibleSpace,
moreBtnItem,flexibleSpace,
nil]];
[self.view addSubview:_bottomBar];
}
Here is a sample selector:
- (void)loadWeather
{
NSLog(#"Loading weather");
}
thanks for the help, but I just realized that it was something I was doing in my viewDidLayoutSubviews method that was causing the problem.
Try this,
-->First drog and drop UIToolbar to your ViewController.xib
-->connect IBOutlet connection
ViewController.h
IBOutlet UIToolbar *_bottomBar;
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self buildBottomMenu];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)buildBottomMenu
{
UIImage *newsImg = [UIImage imageNamed:#"243-globe"];
UIBarButtonItem *newsBtnItem = [[UIBarButtonItem alloc] initWithImage:newsImg style:UIBarButtonItemStylePlain target:self action:#selector(loadHome)];
UIImage *weatherImg = [UIImage imageNamed:#"25-weather"];
UIBarButtonItem *weatherBtnItem = [[UIBarButtonItem alloc] initWithImage:weatherImg style:UIBarButtonItemStylePlain target:self action:#selector(loadWeather)];
UIImage *sportsImg = [UIImage imageNamed:#"63-runner"];
UIBarButtonItem *sportsBtnItem = [[UIBarButtonItem alloc] initWithImage:sportsImg style:UIBarButtonItemStylePlain target:self action:#selector(loadSports)];
UIImage *entrnImg = [UIImage imageNamed:#"178-city"];
UIBarButtonItem *entrnBtnItem = [[UIBarButtonItem alloc] initWithImage:entrnImg style:UIBarButtonItemStylePlain target:self action:#selector(loadEntertainment)];
UIImage *videoImg = [UIImage imageNamed:#"69-display"];
UIBarButtonItem *videoBtnItem = [[UIBarButtonItem alloc] initWithImage:videoImg style:UIBarButtonItemStylePlain target:self action:#selector(loadVideoPage)];
UIImage *moreImg = [UIImage imageNamed:#"259-list"];
UIBarButtonItem *moreBtnItem = [[UIBarButtonItem alloc] initWithImage:moreImg style:UIBarButtonItemStylePlain target:self action:#selector(loadOptionsPage)];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
[_bottomBar setItems:[NSArray arrayWithObjects:flexibleSpace,newsBtnItem,flexibleSpace,
weatherBtnItem,flexibleSpace,
sportsBtnItem,flexibleSpace,
entrnBtnItem,flexibleSpace,
videoBtnItem,flexibleSpace,
moreBtnItem,flexibleSpace,
nil]];
[self.view addSubview:_bottomBar];
}
- (void)loadWeather
{
NSLog(#"Loading weather");
}
Related
I create a toolbar programmatically and add four UIBarButtonItem also programmatically in the toolbar. This button will appear when text view begin editing, if no text in textview clear button and translate button will disable. Here is my four button creating code.
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.backgroundColor = [UIColor lightGrayColor];
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Hide" style:UIBarButtonItemStyleDone target:self action:#selector(cancelKeyboard)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Clear" style:UIBarButtonItemStyleDone target:self action:#selector(clearTextView)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Paste" style:UIBarButtonItemStyleDone target:self action:#selector(paste)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Translate" style:UIBarButtonItemStyleDone target:self action:#selector(translate)],
nil];
[numberToolbar sizeToFit];
_sorceTextview.inputAccessoryView = numberToolbar;
_sorceTextview.autocorrectionType = UITextAutocorrectionTypeNo;
now how can i disable clear and translate button in:
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {}
method? plz help.
Use instance variables for those buttons. Then you can set the enabled property as needed.
#implementation MyViewController {
UIBarButtonItem *_btnClear;
UIBarButtonItem *_btnTranslate;
}
Then in your toolbar setup code:
_btnClear = [[UIBarButtonItem alloc]initWithTitle:#"Clear" style:UIBarButtonItemStyleDone target:self action:#selector(clearTextView)];
_btnTranslate = [[UIBarButtonItem alloc]initWithTitle:#"Translate" style:UIBarButtonItemStyleDone target:self action:#selector(translate)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
numberToolbar.items = #[
[[UIBarButtonItem alloc]initWithTitle:#"Hide" style:UIBarButtonItemStyleDone target:self action:#selector(cancelKeyboard)],
flex,
_btnClear,
flex,
[[UIBarButtonItem alloc]initWithTitle:#"Paste" style:UIBarButtonItemStyleDone target:self action:#selector(paste)],
flex,
_btnTranslate
];
Then wherever you need to disable you can do:
_btnClear.enabled = NO;
and to enable:
_btnClear.enabled = YES;
I set two navigation bar buttons, and there is a space between it, how can I change this space to set buttons closer to each other?
I've tried to add third button with minus width, Here the code
self.editButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Edit.png"] style:UIBarButtonItemStylePlain target:self action:#selector(editAction:)];
self.callButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"call_icon.png"] style:UIBarButtonItemStylePlain target:self action:#selector(editAction:)];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:nil] style:UIBarButtonItemStylePlain target:self action:nil];
spacer.width = -30;
NSArray *buttons = #[self.editButton, spacer, self.callButton];
self.navigationItem.rightBarButtonItems = buttons;
But it is not working; Any suggessions?
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = -16;// it was -6 in iOS 6
[self.navigationItem rightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer, requriedButton/*this will be the button which u actually need*/, nil] animated:NO];
hope this will work for you...
You can create a UIBarButtonItem with the style UIBarButtonSystemItemFixedSpace and add it to the buttons array.
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 ];
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];
I am adding two button in navigation bar they are working fine but i want space between them they are both combined i want a bit space between them
UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc] initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(Add)];
UIBarButtonItem *btnEdit = [[UIBarButtonItem alloc] initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(Add)];
UIToolbar *rightToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
rightToolBar.backgroundColor = [UIColor clearColor];
rightToolBar.tintColor = [UIColor colorWithRed:40.0/255.0 green:48.0/255.0 blue:51.0/255.0 alpha:0.0];
NSArray *buttonsRight = [NSArray arrayWithObjects:btnEdit, btnAdd, nil];
[rightToolBar setItems:buttonsRight];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightToolBar];
You can add any of these two between your UIBarButtonItem
UIBarButtonItem *fixed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]
Note that to set the width of a Fixed Space UIBarButtonItem, you need to set the .width property
[fixed setWidth:455.0f];