Done button on UIToolBar does not operate - iOS - ios

I am trying to create a picker with bar that has done button.
I tried to implement as follows;
viewForDatePicker = [[UIView alloc]initWithFrame:CGRectMake(0, 300, 320, 266)];
[viewForDatePicker setBackgroundColor:[UIColor whiteColor]];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(doneButtonPressed:)];
UIButton * doneButton =[[UIButton alloc]initWithFrame:CGRectMake(290, 2, 30, 20)];
[doneButton setBackgroundColor:[UIColor redColor]];
[doneButton addTarget:self action:#selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[viewForDatePicker addSubview:doneButton];
[toolBar setItems:[NSArray arrayWithObject:btn]];
[viewForDatePicker addSubview:toolBar];
birthDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 320, 266)];
[birthDatePicker setDatePickerMode:UIDatePickerModeDate];
[birthDatePicker setBackgroundColor:[UIColor whiteColor]];
[viewForDatePicker addSubview:birthDatePicker];
[self.view addSubview:viewForDatePicker];
Unfortunately done button does not perform. What is wrong with this code?
Could you please help me

You are adding the button to the view first.
So when you add the date picker it is above the button. This is why the button is not responding to touch.
Change to this:
birthDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 320, 266)];
[birthDatePicker setDatePickerMode:UIDatePickerModeDate];
[birthDatePicker setBackgroundColor:[UIColor whiteColor]];
[viewForDatePicker addSubview:birthDatePicker];
UIButton * doneButton =[[UIButton alloc]initWithFrame:CGRectMake(290, 2, 30, 20)];
[doneButton setBackgroundColor:[UIColor redColor]];
[doneButton addTarget:self action:#selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[viewForDatePicker addSubview:doneButton];
[toolBar setItems:[NSArray arrayWithObject:btn]];
[viewForDatePicker addSubview:toolBar];

Related

Navigation doesn't respond on iPad with iOS 8

Idea is very simple. I have navigation bar in universal storyboards with custom controls.
UIButton *menu = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
[menu setImage:[UIImage imageNamed:#"menu"] forState:UIControlStateNormal];
[menu addTarget:self.viewDeckController action:#selector(toggleLeftView) forControlEvents:UIControlEventTouchUpInside];
UIButton *refresh = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[refresh setImage:[UIImage imageNamed:#"refresh"] forState:UIControlStateNormal];
[refresh addTarget:self action:#selector(reloadPosts) forControlEvents:UIControlEventTouchUpInside];
UIButton *share = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[share setImage:[UIImage imageNamed:#"share"] forState:UIControlStateNormal];
[share addTarget:self action:#selector(showActionView) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *menuBtn = [[UIBarButtonItem alloc] initWithCustomView:menu];
UIBarButtonItem *refreshBtn = [[UIBarButtonItem alloc] initWithCustomView:refresh];
UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithCustomView:share];
UIBarButtonItem *negativeSpacerLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacerLeft setWidth:-10];
UIBarButtonItem *negativeSpacerRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacerRight setWidth:-10];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerLeft, menuBtn, nil];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacerRight, shareBtn, refreshBtn, nil];
Everything works correctly - navigation bar has implemented style and buttons/icons. But there is a problem with iPad and iOS 8 (works on iOS7). When I tap on any button, it does not respond. What I did wrong? It works as well on iPhone with iOS 7/8.

Space between uibarbuttons in navigation bar, Xcode 6.1

I have tried almost all answers here, How to remove the blank space between the two uibarbuttons shown below the code i use is
UIButton *backBTN= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[backBTN addTarget:self action:#selector(backACT:) forControlEvents:UIControlEventTouchUpInside];
[backBTN setBackgroundImage:[UIImage imageNamed:#"menu_icon.png"] forState:UIControlStateNormal];
UIBarButtonItem *backkb = [[UIBarButtonItem alloc] initWithCustomView:backBTN];
// self.navigationItem.leftBarButtonItem = backkb;
UIButton *homeBTN= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[homeBTN addTarget:self action:#selector(homeACT:) forControlEvents:UIControlEventTouchUpInside];
[homeBTN setBackgroundImage:[UIImage imageNamed:#"logout_btn.png"] forState:UIControlStateNormal];
UIBarButtonItem *btnhome = [[UIBarButtonItem alloc] initWithCustomView:homeBTN];
// btnhome.imageInsets = UIEdgeInsetsMake(-10, 0, 0, 0);
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects: backkb, btnhome, nil]];
self.navigationItem.hidesBackButton = YES;
Try this,
// Create a UIView to add both buttons
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
UIButton *backBTN= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[backBTN addTarget:self action:#selector(backACT:) forControlEvents:UIControlEventTouchUpInside];
[backBTN setBackgroundImage:[UIImage imageNamed:#"menu_icon.png"] forState:UIControlStateNormal];
//Add button to UIView
[leftView addSubview:backBTN];
UIButton *homeBTN= [[UIButton alloc] initWithFrame:CGRectMake(30, 0, 30, 30)];
[homeBTN addTarget:self action:#selector(homeACT:) forControlEvents:UIControlEventTouchUpInside];
[homeBTN setBackgroundImage:[UIImage imageNamed:#"logout_btn.png"] forState:UIControlStateNormal];
//Add button to UIView
[leftView addSubview:homeBTN];
//Set UIView as CustomView for bar button
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftView];
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects: leftBarButton, nil]];
self.navigationItem.hidesBackButton = YES;
I have tried this and got output as
used some background colour to know the borders

How to add more zhan two UIBarButtonItem to rightBarButtonItem?

I want add three buttons to rightBarButtonItem , but it just display two.
I also try to add a UIView(buttons in the view) to self.view ,but its below the navigation.
Something Like this:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addButton:)];
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
target:self
action:#selector(showAllButton:)];
UIBarButtonItem *searchButton2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
target:self
action:#selector(showAllButton:)];
self.navigationItem.rightBarButtonItems = #[searchButton, addButton, searchButton2];
YourViewontroller.h
UIView *view_navigation;
viewcontroller.m
- (void)viewDidLoad
{
view_navigation = [[UIView alloc]initWithFrame:CGRectMake(200, 5, 100, 40)];
view_navigation.backgroundColor = [UIColor clearColor];
UIButton *add_button = [UIButton buttonWithType:UIButtonTypeCustom];
[add_button setBackgroundImage:[UIImage imageNamed:#"Add-icon.png"] forState:UIControlStateNormal];
[add_button addTarget:self action:#selector(Add_Student:) forControlEvents:UIControlEventTouchUpInside];
[add_button.imageView setContentMode:UIViewContentModeScaleToFill];
[add_button setFrame:CGRectMake(0, 8, 25, 25)];
[view_navigation addSubview:add_button];
UIButton *Remove_button = [UIButton buttonWithType:UIButtonTypeCustom];
[Remove_button setBackgroundImage:[UIImage imageNamed:#"Delete_icon.jpeg"] forState:UIControlStateNormal];
[Remove_button addTarget:self action:#selector(Remove_Student:) forControlEvents:UIControlEventTouchUpInside];
[Remove_button.imageView setContentMode:UIViewContentModeScaleToFill];
[Remove_button setFrame:CGRectMake(40, 8, 25, 25)];
[view_navigation addSubview:Remove_button];
UIButton *Edit_button = [UIButton buttonWithType:UIButtonTypeCustom];
[Edit_button setBackgroundImage:[UIImage imageNamed:#"Edit_icon.jpeg"] forState:UIControlStateNormal];
[Edit_button addTarget:self action:#selector(Edit_Student:) forControlEvents:UIControlEventTouchUpInside];
[Edit_button.imageView setContentMode:UIViewContentModeScaleToFill];
[Edit_button setFrame:CGRectMake(80, 8, 25, 25)];
[view_navigation addSubview:Edit_button];
[self.navigationController.navigationBar addSubview:view_navigation];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewWillDisappear:(BOOL)animated
{
[view_navigation removeFromSuperview];
[super viewWillDisappear:animated];
}

Why my UIButton doesn't display in my subview? iPad App

Could somebody help me to understand why my UIButton doesn't display in my custom UIView?
In the code below I posted initWithFrame method:
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor =[UIColor whiteColor];
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,frame.size.width, 50)];
toolbar.backgroundColor = [UIColor grayColor];
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(dismissNewInvoiceView:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveNewInvoice:)];
[saveBarButton setEnabled:NO];
NSArray *arrayOfItems = [[NSArray alloc]initWithObjects:cancelBarButton,flexibleSpace, saveBarButton, nil];
[toolbar setItems:arrayOfItems];
UILabel *headerViewLabel = [[UILabel alloc]initWithFrame:CGRectMake(335, 15, 200, 20)];
headerViewLabel.text = #"New Invoice";
[toolbar addSubview:headerViewLabel];
[self addSubview:toolbar];
UIButton *selectCustomerButton = [[UIButton alloc]initWithFrame:CGRectMake(25, 200, 60, 60)];
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"];
[self addSubview:selectCustomerButton];
[self bringSubviewToFront:selectCustomerButton];
UILabel *invoiceNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, 55, 150, 25)];
[invoiceNumberLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
invoiceNumberLabel.text = #"Invoice number:";
[self addSubview:invoiceNumberLabel];
UITextField *invoiceNumberField = [[UITextField alloc]initWithFrame:CGRectMake(140, 53, 150, 30)];
[invoiceNumberField setFont:[UIFont fontWithName:#"Verdana" size:25]];
invoiceNumberField.placeholder = #"25/13";
[self addSubview:invoiceNumberField];
UILabel *dateOfCreationLabel = [[UILabel alloc]initWithFrame:CGRectMake(500, 55, 150, 25)];
[dateOfCreationLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
dateOfCreationLabel.text = #"Date of creation:";
[self addSubview:dateOfCreationLabel];
}
return self;}
Why UILabels are displaying properly by implement addSubview method while UIButtons are not?
Regards
Don't do this:
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"]
Use
[selectCustomerButton setImage:[UIImage imageNamed:#"Farmer.png"] forState:UIControlStateNormal];
The button sets its own image views image depending on its state, you don't access and set the image view's image yourself.
The button will be just invisible in case the image is not loaded/shown properly. As jrturton already said, you should use another method to set the image.
To figure out if the button is 'there', you can give the button a backgroundcolor like this:
selectCustomerButton.backgroundColor = [UIColor redColor];
If you see a red box, you know the image is not properly loaded or maybe not even found.

Label keeps disappearing from my navigation menu

For some reason the label in my navigation bar keeps disappearing and only displaying my map icon and home button. Is it because I'm adding it as a barbuttonitem? Is there a way around this?
UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(160, 5, 250, 70)];
UILabel *l1=[[UILabel alloc]initWithFrame:CGRectMake(0,5, 310, 70)];
l1.text=#"FIND THINGS TO DO";
l1.font=[UIFont fontWithName:#"Helvetica Neue LT Std" size:25.0];
l1.backgroundColor=[UIColor clearColor];
l1.textColor=[UIColor whiteColor];
l1.textAlignment=UITextAlignmentCenter;
[view1 addSubview:l1];
UIBarButtonItem *left2=[[UIBarButtonItem alloc]initWithCustomView:view1];
self.navigationItem.rightBarButtonItem=left2;
[self.navigationItem setHidesBackButton:YES];
self.navigationItem.hidesBackButton=YES;
self.navigationController.navigationBarHidden = NO;
UIImage* image3 = [UIImage imageNamed:#"home_btn.png"];
homeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 18, 38, 38)];
[homeBtn setBackgroundImage:image3 forState:UIControlStateNormal];
[homeBtn addTarget:self action:#selector(Home)
forControlEvents:UIControlEventTouchUpInside];
[homeBtn setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *home = [[UIBarButtonItem alloc] initWithCustomView:homeBtn];
NSArray *arr=[[NSArray alloc]initWithObjects:home, nil];
self.navigationItem.rightBarButtonItems = arr;
UIImage* mapButton1 = [UIImage imageNamed:#"Top-Map-Icon.png"];
mapButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 18, 26, 30)];
[mapButton setBackgroundImage:mapButton1 forState:UIControlStateNormal];
[mapButton addTarget:self action:#selector(Map)
forControlEvents:UIControlEventTouchUpInside];
[mapButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *map = [[UIBarButtonItem alloc] initWithCustomView:mapButton];
NSArray *arr2 = [[NSArray alloc] initWithObjects:map, nil];
self.navigationItem.leftBarButtonItems = arr2;

Resources