Objective C number keypad, custom comma button - iphone

I'm trying to add a comma button on a number keypad that has previous, next and done buttons, so that it looks like this :
Problem is, that the orange button does not respond to taps. Actually if I moved it above the "7" button and tapped it, the 7 would be tapped. So my orange button may be shown above the keyboard, but it's reacting to taps as if it were below.
Here is an abstract of my code :
-(void)textFieldDidBeginEditing:(UITextField *)textField{
UIView *inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 500, screenWidth, 40.0)];
// 3 buttons on top
UIView *topToolbar = [[UIView alloc] initWithFrame:CGRectMake(0, 0.0, screenWidth, 40.0)];
[topToolbar addSubview:self.inputPrevButton];
[topToolbar addSubview:self.inputNextButton];
[topToolbar addSubview:self.inputDoneButton];
[inputAccessoryView addSubview:topToolbar];
// Comma button
UIButton *commaButton = [UIButton buttonWithType:UIButtonTypeCustom];
[commaButton setFrame: CGRectMake(0, 204, 105, 52)];
[commaButton setTitle:#"." forState:UIControlStateNormal];
[commaButton addTarget:self action:#selector(addComma) forControlEvents:UIControlEventTouchUpInside];
[commaButton setBackgroundColor:[UIColor orangeColor]];
[inputAccessoryView addSubview:commaButton];
// Useless, but tried anyway
[inputAccessoryView bringSubviewToFront:commaButton];
[textField setInputAccessoryView:self.inputAccessoryView];
}
All I know is that I should not be doing this (which is actually not working), so what should I do ?

The answer was given by rmaddy in the comments : no need to add a custom comma as a keybord with the comma exists : UIKeyboardTypeDecimalPad, or in the storyboard :

UITextField* tf = TEXTFIELDNAME;
tf.keyboardType = UIKeyboardTypeDecimalPad;

Related

Adding UIButton to subView

When the User taps on the screen a PopUp should appear with a button in it.
But i don't know why the button isn't shown in the PopUp. Is there a problem because it is a subview in a subview ?
-(void) popUpWithX:(int)x andY:(int)y {
CGRect popUpRect = CGRectMake(x, y, 125, 75);
popUp = [[UIView alloc] initWithFrame:popUpRect];
popUp.backgroundColor = [UIColor whiteColor];
popUp.layer.cornerRadius = 7.5f;
popUp.layer.masksToBounds = YES;
[self.view addSubview:popUp];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[button setTitle:#"Click me!" forState:UIControlStateNormal];
[popUp addSubview:button];
}
EDIT:
Is it possible that the coordinates of the UIButton are wrong ? I am not sure if the coordinate system is from the main view or from the popUp subview.
Button is there, but is not visible because of maskToBounds set to YES. Try it just set to NO just for testing purposes. Then fix your x, y coordinates for button.
If the button is going to be a subview of a subview, then you need to add the button prior to adding the view containing it to the main view....i.e. you're adding the button too late.
//Move this line to the end of the block
[self.view addSubview:popUp];//call this after you add your subViews to popUp

Is there anyway to create a UITableView with one universal footer?

I'm trying to create a UITableView with multiple sections. Each section has it's own header, but I am trying to make a universal footer for the entire table view that stays in one position...
Is this logic possible using the UITableViewDelegate methods? Or should I create a custom view and just try and add it as a subview to my table view? The footer I currently have contains a UIButton.
If anyone has some sample code that would be great.
Edit: This question is not the same as the one referenced. I am trying to make a universal footer that floats above the UITableView. The other question does not specify the location of the footer, only that a footer is desired.
read about UITableView's tableFooterView property.
And now some code: (using ARC)
UILabel *footer = [UILabel alloc] init];
footer.text = #"Some text" ;
footer.backgroundColor = [UIColor greenColor];
self.tableView.tableFooterView = footer;
Now at the bottom of entire tableview there is a UILabel that is green with some text.
I ended up creating a subview and adding my button to that view. I then made that view my "footer".
Here is the code that gave me the desired results.
- (void)viewDidLoad
{
[super viewDidLoad];
//self.tableView.delegate = self;
//self.tableView.dataSource = self;
//save current tableview, then replace view with a regular uiview
self.tableView = (UITableView*)self.view;
UIView *replacementView = [[UIView alloc] initWithFrame:self.tableView.frame];
self.view = replacementView;
[self.view addSubview:self.tableView];
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 370, 320, 45)];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.userInteractionEnabled = YES;
//the button should be as big as a table view cell
//width of the button can be set but the width of the view it is added to will always match the width of the tableView
[button setFrame:CGRectMake(60, 0, 200, 45)];
//set title, font size and font color
[button setTitle:#"Build" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//set action of the button
[button addTarget:self action:#selector(buildThenSegue)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[footerView addSubview:button];
footerView.userInteractionEnabled = YES;
[self.view addSubview:footerView];
self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
Take note that this is in a subclass of UITableViewController.
I referenced this answer to another question: https://stackoverflow.com/a/9084267/1091868

UISearchBar with UINavigationController

I added a UISearchBar and UISearchDisplayController with a UINavigationController. Because we needed a few buttons in the navigationBar, I created a custom view that contains a few buttons, and put that as the rightBarButtonItem. I notice when I click on the search bar, the keyboard pops up from the bottom, and also moves the UINavigationController's navigationBar off the screen. So now I cannot see what I enter in my UISearchBar. Is there a way to get around this? I've seen other apps that it looks like they use a UINavigationBar + multiple buttons on the LHS or RHS of the navigationBar, so I'm assuming this is possible. Thanks.
UIButton *homeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
UIImage *house = [UIImage imageNamed:#"icon_house.png"];
[homeButton setImage:house forState:UIControlStateNormal];
[homeButton addTarget:self action:#selector(homePressed:) forControlEvents:UIControlEventTouchUpInside];
UIButton *filterButton = [[UIButton alloc] initWithFrame:CGRectMake(49, 0, 44, 44)];
[filterButton setTitle:#"Filter" forState:UIControlStateNormal];
[filterButton addTarget:self action:#selector(FilterButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.SearchEntry = [[[UISearchBar alloc] initWithFrame:CGRectMake(98, 0, 150, 44)] autorelease];
self.SearchEntry.delegate = self;
UISearchDisplayController *searchDisplayCtlr = [[UISearchDisplayController alloc] initWithSearchBar:_searchEntry contentsController:self];
searchDisplayCtlr.searchResultsDataSource = self;
searchDisplayCtlr.searchResultsDelegate = self;
searchDisplayCtlr.delegate = self;
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 248, 44)];
[containerView addSubview:homeButton];
[containerView addSubview:filterButton];
[containerView addSubview:_searchEntry];
Try to adjust the contentSize of the UIScrollView. Also please take care of the Hierarchy of the controls you have taken. Dont keep UINavigationBar and UISearchView in ScrollView, then it will not move and remains fixed in their position

Smaller active area for custom UIBarButtonItem

I have a UINavigationBar with a custom UIBarButtonItem (which uses a UIButton as its custom view). The problem is: the active area of the custom button is much too large, if I tap at least 40 pixels outside the button, it still gets registered as a tap on the button. This results in accidental taps. How can I reduce the active area on these buttons?
I noticed this weirdness too. I found that using a container UIView fixes this. For example:
UIButton *menuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[menuButton addTarget:self action:#selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];
[menuButton setImage:[UIImage imageNamed:#"menuIcon"] forState:UIControlStateNormal];
UIView *menuButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[menuButtonContainer addSubview:menuButton];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuButtonContainer];
I think u haven't changed the size of the custom button...
Try doing this...
In Interface builder select the button which u want to reduce the active area and then press "Command+3" or "tools --> Size inspector" in that reduce 'W' and 'H' values...
this will make the custom button smaller and so the active area also get reduced...
~Raviraja
Are you adding the button through Interface Builder or are you doing it programmatically? Either way, you can use this line of code to set the bounds of the image:
yourButton.bounds = CGRectMake( 0, 0, yourImage.size.width, yourImage.size.height );
If you want a full example, here's one I used in one of my apps:
UIImage *image = [UIImage imageNamed:#"audio-off.png"];
UIButton *myMuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
myMuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[myMuteButton setImage:image forState:UIControlStateNormal];
[myMuteButton addTarget:self action:#selector(mute) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *myMuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myMuteButton];
navBar.leftBarButtonItem = myMuteBarButtonItem;
[myMuteBarButtonItem release];

How to create a button similar to "Remove Contact" button on iPhone?

Apple has many big colored buttons, that are not standard. One of them is a delete contact button in address book. Another is Start/Stop button in Timer (Clock application) or End Call button in Phone. They are different, but all have similar appearance.
Question is simple. Is there a way to create these kind of buttons without using background images/screenshots or recreating them from scratch?
Thanks.
You could try Opacity. The new release allows you to draw vector images and generate UIKit-friendly Objective-C code that recreates the drawing.
Well, I've tried many ways to do it, but the most simple was to make a UIButton with a custom style.
If you need a "delete" button in the footer of the table - like in contact or event, here is the code:
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UIButton *newDeleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
newDeleteButton.frame = CGRectMake(10, 10, 300, 43);
UIImage *buttonBackground = [UIImage imageNamed:#"ButtonDelete.png"];
[newDeleteButton setImage:buttonBackground forState:UIControlStateNormal];
[newDeleteButton addTarget:self action:#selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UILabel *deleteTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 39)];
deleteTitle.text = #"Delete";
deleteTitle.font = [UIFont boldSystemFontOfSize:20];
deleteTitle.textColor = [UIColor whiteColor];
deleteTitle.textAlignment = UITextAlignmentCenter;
deleteTitle.backgroundColor = [UIColor clearColor];
[footerView addSubview:newDeleteButton];
[footerView addSubview:deleteTitle];
formTableView.tableFooterView = footerView;
First, you create the view for footer.
Then you make a button with the correct background — just make a screenshot of any button you need and delete letters from it.
Than you create a caption for your button and place it over the button.
The last step is to put the button and caption in the footer view, and put the footer view into the table.