Resizing Iphone ScrollView breaks fields - iphone

I have a form about the size of the screen height and am trying to resize the scroll view's frame when the keyboard pops up, which works just fine. Found a lot of great tutorials on that. But after the keyboard pops up I can only click the first 4 fields (coincidentally the ones above the keyboard). I can still use tab to navigate to the lower ones and click them before the keyboard pops up, but I can't scroll to a lower text field and click on it.
I'm not doing much code at all, but I can't tell why it would do this.
- (void) makeRoomForKeyboard:(NSNotification *)notif {
float keyboardHeight = [[[notif userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
scrollForm.frame = CGRectMake(0, 0, scrollForm.frame.size.width, windowHeight-keyboardHeight);
}
I put one of the fields that wasn't working higher up where the good fields are and it again worked fine. I'm guessing there's a rect or something that I'm not setting that triggers the touch event or something.

Don't change the scrollForm frame, instead set the bottom contentInset.

I think you need to resize the UIScrollView's content area in addition to adjusting the frame.
If your UIScrollView was originally the correct size to display all the content without scrolling, you could say something like this:
scrollForm.contentSize = scrollForm.frame.size;
scrollForm.frame = CGRectMake(0, 0, scrollForm.frame.size.width, windowHeight-keyboardHeight);
That should allow you to scroll to the area of the scroll view covered by the keyboard.

Related

How do I make UIPickerview adjust to screen size?

I have a picker that I have created in my iPhone app, and it runs fine - but the picker is part of an overlay, so some of the values are hard-coded as to how far down and to the left the overlay appears.
So an obvious error occurs when I try and make the comparable iPad version. I opened the iPad storyboard and set everything up, but naturally when I run it, I have the uipickerview appearing in the middle and to the left of the huge iPad screen, wayyy off where it should be.
Is there any way to make an overlay of a picker automatically center and hug the bottom of the screen, no matter what size the screen is, instead of having the location of it hard-coded?
It looks like you'd like the picker to rest at the bottom of the view controller's main view (it's parent, I assume) and be as wide as the view. Try this...
CGRect viewFrame = self.view.frame;
CGRect pickerHeight = self.picker.frame.size.height; // assume you have an outlet called picker
CGRect pickerFrame = CGRectMake(0.0, viewFrame.size.height-pickerHeight,
viewFrame.size.width, pickerHeight);
self.picker.frame = pickerFrame;

IOS UIScrollView: Cannot scroll until the end of the content

I am trying to generate a scroll controller in a window with the UIScrollView class, which will contain numerous UIButtons, placed vertically. I set the size of the scroll view equal to the current view controller's root view, so that the scroll view covers the entire visible window. Then I generate the UIButtons I am going to add to the scroll view: I add each UIButton just in the below of the previous UIButton and I add the height of the current UIButton to a variable called "totalContentHeight". Finally, I set the height of the contentSize of the scroll view to this value, in the following line of code:
self.scrollViewForNewsButtons.contentSize = CGSizeMake(self.view.frame.size.width, totalContentHeight);
totalContentHeight is equal to numOfButtons*eachButtonsHeight after I add all the buttons to the scroll view.
The problem is, in the simulator, when I run the app and scroll until the end of the last button and release the mouse, the last two buttons bounces back such that they lie outside of the visible window. It is somewhat hard to express with mere words, so here are the images:
1)This is what I get when I scrolled until the end of the content and held the content at the last possible position it could be pushed:
2)This is what I get after I released the mouse and the scroll view bounced back to its final position:
As you can see, the last two buttons are drawn outside of the visible area. It is like the scroll view's area covers the whole window plus the button area of the IPhone. I could not find a reasonable explanation for this. Am I setting the area size wrong or am I missing something else?
just set content size with calculation with your total button and its height...For Ex..
float yheight = totalButton * yourButtonHeight;
[yourScrollView setContentSize:CGSizeMake(320, yheight + 44)];
try this code...
And if you set the scrollview frame size the same as
self.view.bounds

How do I stop a UIScrollView from automatically scrolling when a UITextField is touched out of the scrollview's frame?

By default, if a textfield is a child of a scrollview and is touched when it is partially outside of the frame of the parent scrollview, the scrollview scrolls up a little. For example, let's say we have a scroll view with a frame height of 200, but a contentSize height of 260 and we put a text field at position 220. Now I scroll up and position the textfield half inside of the scrollview's frame and let the other half get cut off. If I touch this textfield, I notice that the scrollview automatically scrolls up by a couple pixels before the keyboard comes up. This is problematic because I already have my own scrolling code, so when this happens, it ends up scrolling twice as far as I want it to. Is there any way to remove this default behavior?
It sounds like your scrollview is getting moved in response to the keyboard showing instead of the text field being tapped. Take a look at Apple's Managing the Keyboard documentation; it has a helpful section on scrollviews.
A similar question already has been answered here:
https://stackoverflow.com/a/5673026/550177
You can also try to reset the scrollview's contentOffset when the textfield becomes first responder. Implemement the text field delegate like this:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.scrollView setContentOffset:_scrollView.contentOffset animated:NO];
}
This code removes that automatic animation when the textfield is selected. I've tested with iOS 6.1 in the simulator.

scrollRectToVisible doesn't work with keyboard and (next/previous)toolbar. Please see the picture

scrollRectToVisible doesn't work with keyboard and (next/previous/done)toolbar. Please see the picture.
[scrollview scrollRectToVisible: textFieldRect animated:YES];
The method scrollRectToVisible: is doing the correct thing. It's scrolling the view to the point where the CGRect specified is within the visible section of the view. But, here's the thing - you're positioning another view over the top of the scroll view, so a part of the scroll view's visible area is obscured. The scroll view doesn't know about this, it only knows about it's visible section independant of any other views.
A solution to this may be to offset your textFieldRect CGRect by a given amount, to get the scroll view to scroll a little further in a given direction. You could, for example, use the size of the onscreen keyboard to calculate this offset, or perhaps the size of the translucent view that can be seen in your screenshot?

How to make modal view scrollable?

I have a modal view with a textview and a button. When the keyboard is displayed it covers up some of my UI elements. I'd like my modal to be scrollable so that it can be viewed while the keyboard is displayed. How can I do this?
I've had the same question, and i've played with it a bit,
setting a UIScrollView is not enough, as in the inspector
you should 1st. increase it Hight, then in the attributes, check the following checkboxes:
Scrolling enabled, Bounce Scroll, always bounce vertically.
Edited: Forgot the most inportant thing:
in the size inspector, set the Buttom field (under content) to the size you wish, (960 is twice the regular size)
Use a UIScrollView instead of an ordinary UIView. You can disable scrolling when you don't want the user to be able to with:
[theView setScrollEnabled:NO];