UIkeyboard popup with uitextfield on top (pic included) - objective-c

I am trying to achieve this type of effect where a barbutton is pressed and uikeyboard pops up and right above it brings a uitextfield with it. Please see image attached. Can someone point me to the right about how I can do this?
Thanks.

Use UIToolbar or UIActionSheet
Setup a textField as an inputAccessoryView for the keyboard.
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.barStyle = UIBarStyleBlack;
keyboardDoneButtonView.translucent = YES;
keyboardDoneButtonView.tintColor = nil;
[keyboardDoneButtonView sizeToFit];
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(30, 10, 260, 30)];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:textField, nil]];
masterTextField.inputAccessoryView = keyboardDoneButtonView;
// where masterTextField is the textField is the one you tap, and the keyboard rises up along with the small textField.
masterTextField.returnKeyType = UIReturnKeyDone;
[masterTextField setKeyboardType:UIKeyboardTypeDefault];
update:
For a button, put the above code in the (IBAction) of the button.
And instead of masterTextField, create a UIView and add it as a SubView to your view.

Related

How to make a UIActivityIndicatorView in a UIToolbar tappable

I'm using this code to insert an UIActivityIndicatorView to my toolbar
-(void)addActivityIndicatorToToolbar {
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
activityIndicator.userInteractionEnabled = YES;
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[activityIndicator startAnimating];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
barButton.target = self;
barButton.action = #selector(playButtonPressed:);
NSMutableArray *toolbarItemsMutable = [self.toolbar.items mutableCopy];
[toolbarItemsMutable replaceObjectAtIndex:0 withObject:barButton];
self.toolbar.items = toolbarItemsMutable;
}
However, when I tap the UIActivityIndicatorView the action (playButtonPressed) is not performed.
How can I correct this?
It seems more likely that you want a button with an activity indicator inside it. You can do this by creating a button with a custom view as described in this post. Then you can set the action of this button as normal, and you'll probably want to retain a reference to the activity indicator to start and stop it.
I ended up implementing a poor man's solution by adding an extra view on top of the activityIndicatorView with a gestureRecognizer.
This is a quite old question but why don't you directly add a UITapGestureRecognizer instance to your UIActivityIndicatorView instance ? (works fine on iOS 8.2, I didn't test yet on previous versions).

Method called if UIBarButtonItem:initWithImage: is used but not UIBarButtonItem:initWithCustomView:

I have a method that I want to get called when the user clicks on a button in the navigation bar. If I add the button like this then my method gets called:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"myEditButton"]
style:UIBarButtonItemStyleBordered
target:self
action: #selector(enterEditMode:)];
editButton.title = #"Edit";
[self.navigationItem setRightBarButtonItem:editButton animated:YES];
When the user clicks on the button then my enterEditMode: method gets called.
However using this code the result looks like as in the attachment - there's no text but worse my image is lying on top of a blue button. I can't use the standard system edit button because there is a requirement for it to be colored black not blue and AFAIA I'm unable to change the color of the standard edit bar button to black?
So in a xib I created a parent UIView which contains a UIImageView which is the button image and a UILabel for the text.
Then I create the button item like this:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithCustomView:self.editButtonParentView];
[editButton setTarget:self];
[editButton setAction: #selector(enterEditMode:)];
editButton.style = UIBarButtonItemStyleBordered;
editButton.target = self;
self.editButtonLabel.text = #"Edit"
[self.navigationItem setRightBarButtonItem:editButton animated:YES];
Where editButtonParentView is an IBOutlet to the partent view in the nib.
This displays perfectly, however the problem is that enterEditMode: does not get called when the user clicks on it.
Why does it not get called?
Thanks
This link is where I found out how you can get the same look as the system 'Edit' button but get any background color you want. Try this code:
// From:
#import "UIBarButtomItem+Tinted.h"
#implementation UIBarButtonItem (Tinted)
+ (UIBarButtonItem *)newBarButtonItemWithTint:(UIColor*)color andTitle:(NSString*)itemTitle andTarget:(id)theTarget andSelector:(SEL)selector
{
UISegmentedControl *button = [[UISegmentedControl alloc] initWithItems:#[itemTitle]];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = color;
[button addTarget:theTarget action:selector forControlEvents:UIControlEventValueChanged];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
#end

Adding new view into another subview

I am learning iOS. I m using Xcode 4.3.2 , and i have created a button, and in the callback, i have used the following code.
1) Created single view application
2) creating a new view -> newV
3) creating a button but;
4) adding but as a subview of newV.
5) adding newV as a subview of main view.
-(IBAction) submitButtonPressed:(id)sender
{
NSLog (#" Submit Button is pressed ");
UIView *newV = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
newV.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
UIButton *but = [[UIButton alloc] init];
[but setTitle:#"SubView" forState:UIControlStateNormal];
[newV addSubview:but];
[self.view addSubview:newV];
}
But the problem i get is, only the view viewV is shown, but button is not shown. How to resolve this issue ?
When you created the button, you didn't provide a frame. Try using:
UIButton* but = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
When you created the button, it was of zero size, so you couldn't see it.
Try adding the button after adding in the view newV.

Adding UISearchBar programatically - Bar is Off Screen at Launch - iPad

I have added a UISearchBar to a table in my iPad app using the below code. The table is on the left hand side of my split view controller.
The problem is that when the app starts the search bar is strangely off screen to the left - you can just see the last few pixels of it. If you click on it, or scroll down and back up, it reverts itself and looks like it should.
Can anyone suggest how to fix this ?
Thanks
searchBar = [[UISearchBar alloc] init];
self.tableView.tableHeaderView = searchBar;
searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar
contentsController:self];
searchBar.delegate = self;
searchController.delegate = self;
searchController.searchResultsDelegate=self;
searchController.searchResultsDataSource=self;
You can try to create the UISearchBar with a frame like:
CGRect searchViewFrame = CGRectMake(33, 33, 264, 31);
or whatever dimensions or origin.
Like other resource, you can create the UISearchBar within a UIView, and add this UIView to the UITableView:
UIView *containerSearch = [[UIView alloc] initWithFrame: searchViewFrame];
searchBar = [[UISearchBar alloc] init];
[containerSearch addSubview: searchBar];
self.tableView.tableHeaderView = containerSearch;

UINavigationBar custom title being hidden by background

I am adding a custom UIImageView to my UINavigationBar using this code:
UIImageView *background = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
[background setImage:[UIImage imageNamed:#"nav_background.png"]];
[background setTag:kTagForReference];
[self.navigationController.navigationBar insertSubview:background atIndex:0];
I then add a custom title using this code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
...
[self.navigationItem setTitleView:label];
[label release];
But on some of my pushed views the title is being hidden (or isn't visible). I can't seem to push more than two views onto the stack without the title disappearing.
I've tried to force my UIImageView background to the back of the bar but that still doesn't help. I've printed the subviews of the UINavigationBar and I can see that the UILabel is there, I just can't see it!
Any help would be greatly appreciated.
If you can run the dumpWindows() function, the output would show the view hierarchy so you can see what view is covering it up.
You most likely want to use a UIToolBar instead of a NavigationBar. Then you can add the subviews to the UIToolbar.
Bring the UILabel to the front most view.
[self.view bringSubviewToFront: label];