iphone: scroll in a view - iphone

I would like to ask a basic iphone question. I have many TextFields in iPhone view, when I tap to input in a TextField, the Keyboard shows up and hide other TextField. I would like to make the parent view scrollable. Would you please show me the example code?
Thank you very much

You can listen for the keyboard up and down notification. and move your view just above height of keyboard.
In the ViewWillAppear method:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:#selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];
In the ViewWillDisAppear method:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
And then have methods referred to above to adjust the position of the bar:
-(void) keyboardWillShow:(NSNotification *) note
{
CGRect r = bar.frame, t;
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
r.origin.y -= t.size.height;
bar.frame = r;
}
-(void) keyboardWillHide:(NSNotification *) note
{
CGRect r = bar.frame, t;
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
r.origin.y += t.size.height;
bar.frame = r;
}

This will give you an idea
How to make a UITextField move up when keyboard is present?

If the parentview is UIScrollView then try something like in textfield delegate
- (BOOL) textFieldShouldReturn:(UITextField *)theTextField
{
if (theTextField == textFieldName) {
[scroll scrollRectToVisible:CGRectMake(0, 160, 280, 440) animated:YES];//choose the rect accordingly.
}
return YES;
}

Related

How to Use UIScrollView with Search Bar and Search Display Controller?

I am trying to add UIScrollView in Search Bar and Search Display Controller but the problem is that Search Bar gets hidden when I click on it. Please give some suggestion to resolve this.
I am using the following keyboard show/hide notifications.
- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyBoardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)deregisterFromKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyBoardWasShown {
CGPoint buttonOrigin = self.txtfield2.frame.origin;
CGFloat buttonHeight = self.txtfield2.frame.size.height;
CGRect visibleRect = self.view.frame; visibleRect.size.height -= keyboardSize.height;
if (!CGRectContainsPoint(visibleRect, buttonOrigin)){
CGPoint scrollPoint = CGPointMake(0.0, buttonOrigin.y - visibleRect.size.height + buttonHeight);
[self.scrollView setContentOffset:scrollPoint animated:YES];
}
}

keyboard hides, view doesnt scroll down properly only for the first try

I want my view to scroll down when keyboard hides, the thing is that for the first time the view comes from outside of its frame(first image I have attached) but if I hide keyboard for the second time the view scrolls down in a correct way(second figure).
Here is the code related to this part:
- (void)viewDidUnload
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
}
- (void)keyboardDidShow:(NSNotification *)notification
{
[_scrollView setScrollEnabled:TRUE];
}
- (void)keyboardDidHide:(NSNotification *)notification
{
[UIView animateWithDuration:4 animations:^{
NSDictionary* info = [notification userInfo];
//---obtain the size of the keyboard---
NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [self.view convertRect:[aValue CGRectValue] fromView:nil];
//---resize the scroll view back to the original size (without keyboard)---
CGRect viewFrame = [self.view frame];
viewFrame.size.height += keyboardRect.size.height;
self.scrollView.frame = viewFrame;
}];
}

How to expand a UISearchBar when it becomes active

I have a UISearchBar that needs to be compressed (see screenshot) and then expand to a larger size when it is touched or isActive.
How would I go about doing this? Currently my search bar is placed in the view via IB.
thanks
Use this delegate of the search bar :
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[self setTheSearchBarWithRect:newRect];//newRect is CGRect;
}
-(void)setTheSearchBarWithRect:(CGRect)frame{
[UIView animateWithDuration:(1.5f)
delay:0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
yourSearchBar.frame = frame;
}
completion:^(BOOL finished){
}];
}
and in the below delegate call the above function with its original frame.
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar;
I would suggest adding a NSNotifcation listener for the keyboard showing/hiding notifications and based on this adjust the UISearchBar frame:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(adjustFrame:) name:UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:#selector(adjustFrame:) name:UIKeyboardWillHideNotification object:nil];
}
We need to remove the listener when the view is going to disappear:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[nc removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
And now we need the 2 custom functions to adjust the frame:
- (void)adjustFrame:(NSNotification *) notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
if ([[notification name] isEqual:UIKeyboardWillHideNotification]) {
// revert back to the normal state.
self.searchBar.frame = CGRectMake (100,50,100,self.searchBar.frame.size.Height);
}
else {
//resize search bar
self.searchBar.frame = CGRectMake (10,50,200,self.searchBar.frame.size.Height);
}
[UIView commitAnimations];
}
You should just change search bar frame (location and size) on editing start and ending.
for example:
on start
sbar.frame = CGRectMake(sbar.frame.origin.x - 100., sbar.frame.origin.y, sbar.frame.size.x + 100., sbar.frame.size.y);
on edit end just back control at original place:
sbar.frame = CGRectMake(sbar.frame.origin.x + 100., sbar.frame.origin.y, sbar.frame.size.x - 100., sbar.frame.size.y);

Event for dismissing onscreen keyboard by clicking the keyboard button

I've been looking everywhere... maybe i'm not using the right searchwords since i do believe this is a common question.
Is there an event i can handle for when the user dismisses the keyboard by clicking the button to lower the keyboard.
i move a view up when a uitextfield becomes firstresponder but i want to move it down again when this button is tapped
Try using notifications. Add that to your viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
and then create a method called keyboardWillHide:
- (void)keyboardWillHide:(NSNotification *)notification {
//do whatever you need
}
Hope it helps
Check out the second paragraph in the "Managing Keyboard" section: http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UITextField_Class/Reference/UITextField.html
By using NSNotificationCenter you get Keyboard events.You can register for keyboard events in viewWillAppear and don't forget to unregister in viewWillDisapper.
We will use here two notifications :
UIKeyboardDidShowNotification apple docs
UIKeyboardDidHideNotification apple docs
You can do some thing like this:
(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(#"Registering for keyboard events");
// Register for the events
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector (keyboardDidShow:)
name: UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector (keyboardDidHide:)
name: UIKeyboardDidHideNotification
object:nil];
// Setup content size
scrollview.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH,
SCROLLVIEW_CONTENT_HEIGHT);//for e.g. (320,460)
//Initially the keyboard is hidden
keyboardVisible = NO;//in .h declare BOOL keyboardVisible;
}
-(void) viewWillDisappear:(BOOL)animated {
NSLog (#"Unregister for keyboard events");
[[NSNotificationCenter defaultCenter]
removeObserver:self];
}
-(void) keyboardDidShow: (NSNotification *)notif {
NSLog(#"Keyboard is visible");
// If keyboard is visible, return
if (keyboardVisible) {
NSLog(#"Keyboard is already visible. Ignore notification.");
return;
}
// Get the size of the keyboard.
NSDictionary* info = [notif userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Save the current location so we can restore
// when keyboard is dismissed
offset = scrollview.contentOffset; //in .h declare CGPoint offset and UIScrollView *scrollview.;
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = scrollview.frame;
viewFrame.size.height -= keyboardSize.height;
scrollview.frame = viewFrame;
CGRect textFieldRect = [activeField frame];//in .h UITextField *activeField;
textFieldRect.origin.y += 10;
[scrollview scrollRectToVisible:textFieldRect animated:YES];
// Keyboard is now visible
keyboardVisible = YES;
}
-(void) keyboardDidHide: (NSNotification *)notif {
// Is the keyboard already shown
if (!keyboardVisible) {
NSLog(#"Keyboard is already hidden. Ignore notification.");
return;
}
// Reset the frame scroll view to its original value
scrollview.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT);
// Reset the scrollview to previous location
scrollview.contentOffset = offset;
// Keyboard is no longer visible
keyboardVisible = NO;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
hope would help you :)

No UIKeyboardWillShowNotification when tapping the next UITextField

I have several UITextFields listed below each other and would like to move the active field appropriately when the keyboard slides up.
I have added my controller as an observer to UIKeyboardWillShowNotifications and that works fine when the user first taps a text field. If he taps one of the other text fields without making the keyboard go away first no UIKeyboardWillShowNotifications appears though and I get no chance to adjust the position of the new active text field. I guess it somehow makes sense that no UIKeyboardWillShowNotification appears as the keyboard just stays there but...
What to do??
I do not see how I can add my adjusting code somewhere else as I need the keyboard size information which is kept in the userInfo in the keyboard notification.
Thanks a lot,
Stine
EDIT: Maybe it will be more clear what I need if I paste some of my code here:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(cellInputViewWillOpen:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(cellInputViewWillClose) name:UIKeyboardWillHideNotification object:nil];
}
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void) cellInputViewWillOpen:(NSNotification *)aNotification {
CGRect keyboardFrame = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
keyboardFrame = [self.view convertRect:keyboardFrame fromView:nil];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]];
self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, cell.frame.origin.y - ((self.tableView.frame.size.height - keyboardFrame.size.height) / 2.0f));
self.tableView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, keyboardFrame.size.height - 44.0f, 0.0f);
[UIView commitAnimations];
}
- (void) cellInputViewWillClose {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
self.tableView.contentInset = UIEdgeInsetsZero;
[UIView commitAnimations];
}
Use textField:didBeginEditing instead. You may need to keep track separately of if your keyboard is already showing so you don't scroll twice.
EDIT : I didn't realise you were talking about a table view, sorry. In that case you can adjust the scroll view contents as you are already doing, then use the table view scroll to index path methods when you change the selection using the table view delegate methods.