last line of input text extends outside of UIScrollView - ios

I have a UITextView within a UIScrollView in one of my controllers.
The problem is that when you enter text and get to the bottom of the scrollView, the last line of text (the one you're typing) actually extends beyond the bottom of the scroll view and is hidden.
Any help would be much appreciated.
scrollView.frame = CGRectMake(15.0f, 20.0f+15.0f, self.view.frame.size.width-15.0f*2.0f, self.view.frame.size.height-keyboardBar.frame.size.height-keyboardBounds.size.height-20.0f-15.0f*3.0f);
postTextView.frame = CGRectMake(0.0f, 0.0f, scrollView.frame.size.width, scrollView.frame.size.height);
[self.view addSubview:scrollView];
[scrollView addSubview:postTextView];
`

You need to set the content size of the scroll view so that it knows how to scroll its subviews:
[scrollView setContentSize:postTextView.frame.size];
The frame of the scroll view is just the space it takes up by itself on self.view. The content size is the size that all of the scroll view's subviews will take up inside of the scroll view.

Related

Image and Html content on app screen with combined scrolling

Problem
I want to display an image and Html text on the app screen. The text is large so it may not fit on a screen so I want the content to be scrollable. WebView by default has scroll functionality for content going beyond screen size. But I don't want just the text to move upwards as I scroll down but also the image to move upwards i.e. I do not want the image to stick around on the screen when I scroll downwards.
One simple solution for this is to have the image rendered within the html content, but I do not want to do that because ImageView allows me additional functionality that I want to use.
Possible solution
The solution I tried for this was having an ImageView and a WebView within a ScrollView but when I try implementing it, only the webview content is scrolling and the image is fixed. (This is possibly because the WebView's scroll is getting preference over the parent ScrollView's scroll. I even tried setting WebView's child scroll view's delegate to outer scroll View but that didn't help and html text was not scrollable at all after build:
webview.scrollView.delegate = self.scrollView
How can I solve the problem of combined scrolling for ImageView and content in WebView possibly using other container views (if necessary)?
It sounds like you could use a UITableView with a UIImageView as your tableViewHeader. The UIWebView component could be a UITableViewCell or a tableFooterView depending on how you want to architect your view.
Set your UIImageView as your UITableView's tableHeaderView. That way, as the table scrolls, your image will scroll off screen.
Create a UITableViewCell that contains a UIWebView. You can set the height of the cell to whatever your content size is, or the screen size. The webview itself will be scrollable in this cell, and the user also is able to scroll back up to see the image header.
You can try this method :
Disable the scrolling of webview. Place the image on top of webview.
self.yourScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.yourWebView = [[UIWebView alloc] initWithFrame:CGRectMake(x position, y position, width, height)];`
self.yourWebView.opaque = NO;
self.yourWebView.scrollView.scrollEnabled = NO;
self.yourNewsArticleImageData = ({
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x position, y position, width, height)];
imgView.contentMode = UIViewContentModeScaleAspectFit;
imgView.backgroundColor = [UIColor clearColor];
imgView;
});
[self.yourScrollView addSubview:self.yourWebView];
[self.yourScrollView addSubview:self.yourNewsArticleImageData];
self.yourScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
self.yourScrollView.scrollEnabled = YES;
[self.view addSubview:self.yourScrollView];
Main thing you need to do is disable the scrolling of webview and enable scrolling of your scroll view. Put image and webview content as subviews of scrollview.
You can change the height of the scroll view in the webview didFinish delegate method. Once you get the height of the webView loaded content change the "content size" of scroll view accordingly.

how to split the view into halves and addid a scroll view on each half of the view?

I have to divide the view int two,on the first half i have to add a scroll view(i.e to call a specific method in scroll view for setting a page) and on the second half i have to add another scroll view.I have to do this programmatically.Please help.
You can easily split a UIView into two.
If I understand you right I would do something like this
UIScrollView scroller1 = [UIScrollView alloc]initWithFrame:CGRectMake(0,0, splittedView.frame.size.width /2, splittedView.frame.size.height) ];
UIScrollView scroller2 = [UIScrollView alloc]initWithFrame:CGRectMake(splittedView.frame.size.width /2,0, splittedView.frame.size.width /2, splittedView.frame.size.height) ];
[splittedView addSubView: scroller1];
[splittedView addSubView: scroller2];
Now you have a view with two scrollviews on it. And every scrollview has half the size of your main view.

Can't add a UIScrollView to a part of the view

Inside a UIViewController, I need to have the bottom half scrollable. So I added a UIScrollView and positioned it halfway down the view's height. And in the viewDidAppear method, I have put the below two code lines to make it scrollable.
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);
self.scrollView.frame = self.view.frame;
This way works if the scroll view fills the entire view, I've tested. But this method didn't work for my need. The scroll view would automatically move up and take up the entire screen. I assumed it was the second line of code which causes this.
So I removed the scroll view, added two UIViews to the view controller. To the bottom view, I added the UIScrollView. And in the viewDidAppear method, I have put the same two code lines changing the second line to refer the frame of the UIView that contains the scroll view..
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);
self.scrollView.frame = self.containerView.frame;
But it wouldn't scroll either.
Can anyone please tell me how to do this correctly?
Thank you.
Dude, you keep setting the frame of the scrollView to something completely different from what you're actually trying to achieve.
If all you want to do is setup your scroll view so that it only occupies half the space then why dont you just set the frame so that the height only covers the portion of the screen that you want it to cover; and then set the x & y coordinates so that you draw the scroll view from the right position.
Do something like this:
//Shortcut to view's frame.
CGRect viewsFrame = self.view.frame;
/**
CGRectMake takes 4 parameters: x, y, width, height
x: is set to 0 since you want the scrollview to start from the left with no margin
y: you want the y position to start half way, so we grab the view's height and divide by 2
width: you want your scrollview to span from left to right, so simply grab the view's width
height: you want your scrollview's height to be half of your screen height, so get view's height and divide by 2.
*/
CGRect frameForSV = CGRectMake(0, viewsFrame.size.height/2, viewsFrame.size.width, viewsFrame.size.height/2);
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:frameForSV];
[self.view addSubview:myScrollView];
Then set your content size not based on an ansolute value, its best to have it based on the size of the content that's actually inside your scrollview so that your scrollview always scrolls to cover all your content inside it.
Also, remember that your scrollview will only scroll if the contentsize is greater than the scrollview's frame
UPDATE 1 after reading your comment in this post simply comment out any code in your viewController.m file related to your scrollview since youre setting up everything in interface builder.
This is the result:

UIScrollView not scrolling in iOS7 with autolayout on

I have a UIScrollView with a 6 textfields in it and a button inside of it. There is not enough content in the scrollView to make it scroll.
But when the keyboard shows, I would like the scrollview to scroll so the user doesn't have to dismiss the keyboard in order to select another textfield that is hidden by the keyboard.
I am using iOS7 and have autolayout enabled.
Any suggestions?
I am using storyboards and the only code I have is the following.
reg.h file
interface registerViewController : UIViewController <UITextFieldDelegate, UIScrollViewDelegate>
In order to make a scrollview scrollable, the content size must be larger than the scrollview's frame so the scrollview has something to scroll to. Use setContentSize to adjust the content size:
[scrollview setContentSize:CGSizeMake(width, height)];
In this case, you should adjust the size to view.frame.width, view.frame.height + keyboard_height, then adjust the content offset once the keyboard appears:
[scrollview setContentOffset:CGPointMake(0, 0 - keyboard_height)];
If for some screwy, autolayout-related reason this still doesn't make the view scrollable, implement this setContentSize function in viewDidLayoutSubviews in order to override the autolayout:
- (void)viewDidLayoutSubviews {
[scrollview setContentSize:CGSizeMake(width, height)];
}
EDIT: To reset the scrollview after dismissing the keyboard, reset the scrollview content size to the scrollview's frame and the offset to zero:
[scrollview setContentSize:CGSizeMake(scrollview.frame.size.width, scrollview.frame.size.height)];
[scrollview setContentOffset:CGPointZero];
P.S. To animate the content offset, use:
[scrollview setContentOffset:offsetSize animated:YES];
There is a contentInset property of UIScrollViews, you can set the contentInset to make additional space at the bottom to allow for scrolling without changing contentSize.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, 100, 0.0);
scrollView.contentInset = contentInsets;
above code adds 100 points inset at the bottom.
By the way, there is an official document about this matter. It explains everything you should do. You can find it here. You can find what you are looking for under the section 'Moving Content That Is Located Under the Keyboard'
Try
Create Scroll view
Add View to the scroll view (In my case i added view as mainView).
Set ScrollView autoresizing.
Set MainView autoresizing.
To set the Scroll content Size equal to the view created add below line
Add the below line
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.scrollView.contentSize = self.mainView.frame.size;
}

Scrolling a UITextview with UIScrollview

So I've created an UITextView and put it inside an UIScrollView. However, I disabled scrolling for UITextView, so I was wondering is it possible for me to scroll the textview at the same time while I'm scrolling through my UIScrollView?
I'm not sure if someone has already asked something like this, I tried to search through the site and couldn't find any topics related to my problem. If there's an existing post like this I would appreciate it if someone plink me to it.
UIScrollView is a scrolling UIView. So you need just to add the UITextView to the scroll view as a subview and it will scroll with it, basically something like this:
UIScrollView *scrollView = [UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,460)];//adjust the frame to fit your needs
scrollView.contentSize = CGSizeMake(400, 600);//make sure the size in contentSize is bigger than the size of the scrollView frame: (400, 600) is bigger than (320, 460), otherwise the scroll effect won't be performed
UITextView *textView = [UITextView alloc]initWithFrame:CGRectMake(0,0,200,400)];//adjust the frame to fit your needs
[scrollView addSubview:textView];//need to add the text view as a subview
[self.view addSubview:scrollView];

Resources