Shifting UIView while UITextView is editing - iphone

I have a big comment field, which is a UITextView. Since the keyboard appears, and the UITextField is nearly half of the whole view, users can hardly see the field. What I want to do is when the keyboard appears, (i.e. the UITextView is editing) I want to shift the view up to make it completely visible, and user friendly.

You can use the
UITextViewTextDidBeginEditing
to rearrange items in your view. And use
– bringSubviewToFront:
to bring the textview to the front.

After looking through complicated and useless codes, I found a quick solution here:
https://stackoverflow.com/a/5221243/1139419
This answer here handles this situation very gently, thanks to Dan Ray
A quick note regarding: Calling this function not quite did the charm for me
-(void)scrollToView:(UIView *)view {
CGRect theFrame = view.frame;
float y = theFrame.origin.y - 15;
y -= (y/1.7);
[self scrollToY:-y];
}
so I edited this function:
-(void)scrollToView:(UIView *)view withCoefficient:(CGFloat)coeff
{
CGRect theFrame = view.frame;
float y = theFrame.origin.y;
y -= (y/coeff);
[self scrollToY:-y];
}
into this. This way I can try which coefficient for sliding my main view is proper. Instead of 1.7, 2 did the charm for me. Please visit the link for full solution. Also note that you might want to apply same thing for your UINavigationController as well, if exists;
-(void)textViewDidBeginEditing:(UITextView *)textView
{
[self.view scrollToView:textView withCoefficient:2.0];
[self.navigationController.view scrollToView:textView withCoefficient:2.0];
}

Related

Scrolling when the keyboard activated

I have an app where you have textfields and one textview but when I get the keyboard it hides the lower textfields. How would I do it.
I have tried:
.m:
- (void)textFieldDidBeginEditing:(UITextField *)sender {
CGSize content = _scrollView.contentSize;
_scrollView.contentSize = CGSizeMake(content.width, content.height + 200);
svos = _scrollView.contentOffset;
CGPoint pt;
CGRect rc = [sender bounds];
rc = [sender convertRect:rc toView:_scrollView];
pt = rc.origin;
pt.x = 0;
pt.y -= 200;
[_scrollView setContentOffset:pt animated:YES];
}
- (IBAction)textFieldShouldReturn:(UITextField *)textField {
CGSize content = _scrollView.contentSize;
_scrollView.contentSize = CGSizeMake(content.width, content.height - 200);
[_scrollView setContentOffset:svos animated:YES];
[textField resignFirstResponder];
}
.h:
CGPoint svos;
Although the bottom text fields are still hidden it does scroll to the visible ones
You have obtained the origin of the sender textfield but only move up by 60, thus, the lower textfields are covered by the keyboard. You will need to know the height of the keyboard and calculate the distance to move up. Check this out. It has much of the answer so I will not explain again.
To scroll to the bottom textfield inside a scrollview, add these lines in textFieldDidBeginEditing:
CGSize content = _scrollview.contentSize;
_scrollview.contentSize = CGSizeMake(content.width, content.height + 200);
This will extend your contentSize programmatically so you can scroll to the last textfield and allow the keyboard to cover the empty space.
In textFieldDidEndEditing or textFieldShouldReturn, in your case, add these:
CGSize content = _scrollview.contentSize;
_scrollview.contentSize = CGSizeMake(content.width, content.height - 200);
I used an arbitrary 200 as example. You will need to figure out how much you want.
A drop-in universal solution for moving text fields out of the way of the keyboard in iOS
https://github.com/michaeltyson/TPKeyboardAvoiding
It works perfect for me.
Simply you need to copy the required classes(TPKeyboardAvoidingScrollView or TPKeyboardAvoidingTableView) in your project and in your interface builder file you have to change the UIScrollView class to TPKeyboardAvoidingScrollView or UITableView to TPKeyboardAvoidingTableView, the remaining things will be handled by these classes.
Have you seen the documentation about managing the keyboard? (apple documentation) There is an example of what i think you are working on. Hope it helps.

Trying to resize a UITableView glitches the screen for a second before displaying properly

I had a lot of trouble resizing a UITableView to fit between an UINavigationBar and a UITabBar. My implementation involved creating a custom frame in viewDidAppear(), and then setting the UITableView to an appropriate frame. Although this solution works well, it doesn't work perfectly- the screen has a little spasm every time the view is loaded. I figured the issue was due to the UITableViewb becoming fullscreen, as it wants to, and then me resizing it, in front of the user's eyes. However, I have no idea how else to implement what I want to: resizing the UITableView to fit into the screen properly. Here's my implementation in viewDidAppear():
- (void)viewDidAppear:(BOOL)animated{
[self.view.superview addSubview:navigationBar];
CGRect frame = self.view.frame;
frame.size.height = self.view.frame.size.height - navigationBar.frame.size.height;
frame.origin.y = self.view.frame.origin.y + 44;
self.tableView.frame = frame;
[super viewDidAppear:animated];
}
It's very hard to see the effect when recorded on video, and probably terrible in GIF form, but here's a little GIF I recorded of the flash being induced.
Here is the result if I use that same code in viewWillAppear() instead:
Thank you!
I had this problem, turned out it was an issue with auto layout on the view. In the Interface Builder, switch to the File Inspector property view and make sure 'Use Autolayout' is unchecked.
After this, you can consistently resize your UITableView in the viewWillAppear method without the glitches.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGRect frame = [tableView frame];
frame.size.height = 365;
self.tableView.frame = frame;
}
Why using viewDidAppear, by the time this method gets called your view is visible on screen then you will of course receive a glitch.
What you have to do is change the method from viewDidAppear to viewWillAppear.

Using UIScrollview to access 4 different views

This seems like it should be pretty straight forward but im really stuck. I basically want to start the app and it goes to the center view, where the user can swipe up down left or right to access four different views. the picture pretty much sums it up. I will make the text in the picture "swipe up for view 1" "swipe down for ....." buttons also that achieve the same thing as the swiping but I dont want to ask for too much so if anyone can help me out and show me how to program what Im looking for I would much appreciate it.
I was able to get it to be just one massive view but I realized that I want it to jump to every different view, not scroll and be half way there. and when I tried to make cgrect frames it was very confusing to keep it all in order.
Here,I am Providing you sample code.I just write code for two views.You just need to determine the views position according to scroll.
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if(scroll.contentOffset.y> 480 && scroll.contentOffset.x<320)
{
//where next view is you view which you need to display according your requirements.
next *pushnext = [[next alloc]initWithNibName:#"next" bundle:nil];
[self.view addSubview:pushnext.view];
CGRect frame = pushnext.view.frame;
frame.origin.x = 10;
pushnext.view.frame = frame;
[pushnext release];
}
else if(scroll.contentOffset.y<480 && scroll.contentOffset.x>320)
{
//where next view is you view which you need to display according your requirements.
next *pushnext = [[next alloc]initWithNibName:#"next" bundle:nil];
[self.view addSubview:pushnext.view];
CGRect frame = pushnext.view.frame;
frame.origin.x = 10;
pushnext.view.frame = frame;
[pushnext release];
}
}
I hope this will help you.

positioning frame does not work after move to iOS 4.2

I'm having some trouble with moving a view:
- (void)viewDidLoad {
CGRect newFrame = self.popUp.view.frame;
newFrame.origin.y = self.view.bounds.size.height;
self.popUp.view.frame = newFrame;
[[self view] addSubview:[self.popUp view]];
[super viewDidLoad];
}
This should put the subview popUp below the current screen but it does not seem to be moving it. I'm almost positive this was working pre-4.2. Any ideas as to what might be going on? Sorry for the vagueness. If you have any questions feel free to ask.
Did you try moving [super viewDidLoad] to the top of the method? Where you call a superclass's method can affect the state of inherited vars.
Good post on this: `[super viewDidLoad]` convention
It looks like you are moving so the top of popUp.view is at the bottom of self.view. And then when you are adding popUp.view to self.view you are placing it "outside" of self.view. You can verify this by newFrame.origin.y = self.view.bounds.size.height - 100; and see if it shows up.
One reason that your view doesn't show up as it did before could be that you have changed clipsToBounds. I imagine it can also be some other drawing optimization taking place and not drawing views that are out bounds and not visible.
If you want your pop up to show up below a view I think a better approach would be to either place your pop up at the bottom of your view:
newFrame.origin.y = self.view.bounds.size.height - newFrame.size.height;
or by placing the pop up view in the superview of self.view. In that case you would probably want to handle the showing in the controller of the superview.

(iphone) non-editable UITextView "bookmark"

I am new to iphone dev and I'm trying to create a "bookmark" in an NON-editable UITextView. More specifically - I go to the view with the UITextView, scroll maybe halfway(or to some other point) through the text, leave that view and when I come back the UITextView is showing where I left off.
I have everything figured out except how to 'capture' the point that the NON-editable UITextView is scrolled to when leaving the view. I've tried selectedRange in viewWillDisappear in every way I could think of.
Any ideas on this would be greatly appreciated, I've been struggling for almost two days.
I hope I'm explaining that clearly enough.
Thanks in advance!!
GL
h
UITextView *txtView;
CGPoint position;
m
-
-(void) savePosition
{
position = txtView.contentOffset;
}
-(void) restorePosition
{
CGRect r = CGRectMake(position.x, position.y, txtView.contentSize.width, txtView.contentSize.height);
[txtView scrollRectToVisible: r animated: NO];
}