I have a ScrollView within which I am adding TextViews, ImageViews and a UIWebView. I am able to dynamically adjust the height of all these views except the UIWebView.
I have turned off the scrolling of the UIWebView, and want to resize the WebView according to its content. And at the same time resize the scrollview according to the resized WebView.
The scrollview should contain the resized WebView. I think there is much discussion about and around this but I haven't been able to reach a concrete solution. So any help would be appreciated.
here is what you can try
After you have loaded content in the UIWebView you can use it's stringByEvaluatingJavaScriptFromString: method to ask the html document for its height. This is a little tricky but should work.
You can do something like this:
NSInteger height = [[webView stringByEvaluatingJavaScriptFromString:
#"document.body.scrollHeight"] integerValue];
The scrollheight mught not be the best. You have a couple of options. Experiment with the document properties mentioned in http://james.padolsey.com/javascript/get-document-height-cross-browser/
after that u can resize you scrollview to the content,
this is the only method seems working, till now. Good- luck
Related
I've a UIWebview displaying a .rtf file. The lines of text are wider than the screen.
1) Is there a way to get the text to wrap around rather than going off the edge of the screen and requiring the user to scroll.
2) Is there a way to disable horizontal scrolling in the UIWebView.
Many Thanks
Code
If you want the content to fit inside the view, set it's scalesPagesToFit property to true:
webView.scalesPagesToFit = YES;
If you do that, the pages shouldn't be horizontally scrollable. If they are, you can fiddle around with the stuff in UIScrollView Reference because UIWebView is a subclass of it.
How do I make the content in the UIWebView fill the whole screen? The UIWebView fills the iphones width correctly, however I need to doubletap the image in the UIWebview to make it stretch out to the bounds.
Help much appriciated!
Try setting scalesPageToFit to YES on your UIWebView.
I have had to change a UIScrollView into a UIWebView due to formatting reasons.
There is a problem though that I need to be able to get the content offset back to 0, 0 (as I am using reusable UIWebViews and changing content) and get the content size so I can place a button at the bottom; but UIWebView does not seem to have this.
This problem can be solved quite easily:
[[webView scrollView] setContentInset:UIEdgeInsetsMake(64, 0, 44, 0)];
Remember that UIEdgeInset is different from CGRect!
A guide on UIEdgeInset can be found in the iOS Developer Library.
The best way I found to sort the problem was:
Create a UIWebView
Put it in a UIScrollView
Add the HTML into it using "loadHTMLString"
Using the "- (void)webViewDidFinishLoad:(UIWebView *)webView {" delegate method I detect the size of the UIWebView
Set the frame of the UIWebView to the content size.
Remove interaction from the UIWebView
Set the Content size of the UIScrollView to the content size.
Then used the functionality of the UIScrollView instead of the UIWebView.
Thanks
-JM
Use JavaScript.
I think that the next line should do the magic:
[webView stringByEvaluatingJavaScriptFromString:#"window.scroll(0,0)"];
Now you can ask for the contentSize property of the scrollView property of a UIWebView.
This enables you to also change the content size of that web view, using CGSizeMake (width, height).
Once you get the UIScrollView that is inside the UIWebView, it will be easy to change ContentOffset and contentSize. Information here on how to get the scrollView : ScrollOffset in UIWebView?
I'm experiencing something considered a bug in my situation. Probably this is not a bug but a feature ;).
Here's my case:
I load a UIScrollView with my content. A part of my content is loaded asynchrone after the view is already loaded. This works great and without issue.
Some of these controls however are UITextView controls. Once I set the content (text property) of these controls after the viewDidLoad my UIScrollView scrolls down to the last UITextView that was set. I want to prevent this and want the UIScrollView to maintain it's top scrolled position.
In summary:
If I set the Text property in the viewDidLoad method no scroll occurs. If I set the Text property on the UITextView after the viewDidLoad method (because I load the data asynchronous) the UIScrollView will scroll to the last UITextView that was set. I want to prevent this scroll.
How do I achieve this ? I have a feeling this is just a property which is set wrong but I can't seem to find it.
Thanks in advance.
EDIT:
I've tried setting the "scrollEnabled" property to NO before setting the values and to YES after but this didn't have any effect. The ScrollView will still scroll when the text property of the UITextView is set.
I Fixed the issue with a work-around:
I set the scroll view content size to something small, like 320 x 300 or whatever the height of the scrollview frame is, then did my work, then put the content size back to normal.
This prevents the scrolling while the data is loaded and enables it as soon as the loading is finished.
This would not change the scrolling problem but maybe make it "hidden" for the user:
yourTextView.text = #"Eat more bananas!";
yourScrollView.contentOffset = CGPointMake(0.0, 0.0);
Some snippets of your code would help to face the problem more specific.
EDIT:
Or try to add the UITextViews to a UIView, then add the UIView to the UIScrollView. Make sure that the UIScrollView's contentSize is the same as the Size of the UIView.
This worked for me too :p but i had to set the height to something less then 300 (in my case i just used 10).
Basically the idea is to make the text view not part of the visible area of the UIScrollView wile you are changing the text of th UITextView.
I want to display a text with a lot of lines. I added a multiline-label to a scroll view, but it didn't show anything. Looks like this is not the correct way to use the scroll view. How to use scroll view so that users can drag down to see more text?
Apple's UIScollView documentation is quite good, you should start there and understand the class.
Think of a Scrollview as a large view surface over which the screen slides*. You can add subviews to the scrollview and then scrolling is like positioning the screen over the scrollview - the screen acts like a window you can see through to part of the scroll view's content below.
To do this, a scrollview has a few extra properties over a normal UIView - but it is like a UIView in one imprtant respect: it doesn't render any content directly itself. You need to add a subview to draw your text. A scrollview is set up and displayed in exactly the same way as a UIView - i.e. you set the frame add it to another view and to show your text you need to add subviews to the UIScrollView that can actualy render the text.
In order to set up a basic UIScrollView you request, you should just create it like a normal full screen view - set the frame to be the same size as the window and add the scrollview to the window as a subview. Then make a large UITextView to hold your text. The UIText view can be as large as you like - specifically it can be bigger than the screen. set the contentSize property of the UIScrollView to be the same as the frame of the UITextView and then add the UIText view as a subview of the UIScrollView.
Once you have this working, you can move the content automatically with the contentOffset property, controll zooming and set up a delegate to observe scrolling events.
* more accurately, over which the frame slides but I'm assuming you are making a full screen UIScrollView. I'm sure you can generalise it if you want a smaller view.
You can just use a UITextView. It's a child of UIScrollView, and if you set it's text property to an amount of text that does not fit within it's frame, the view will become scrollable.
I used UIScrollView this way for an instructions pane in one of my applications. It worked but I was unhappy with the result. I then switched to using a UIWebView and have been much happier with the result. It handles sizing the scroll view automatically, I get all the formatting capabilities of HTML and I can have links that go to additional information (or external sites) with no more than adding another HTML file and maybe some delegate code. Here is the code I used (and I added the HTML file to my app as a resource):
NSString *path = [[NSBundle mainBundle] pathForResource:#"instructions" ofType:#"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData: [readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
[self.instructionsView loadHTMLString:htmlString baseURL:nil];
U may refer to this website http://www.edumobile.org/iphone/iphone-programming-tutorials/scrollview-example-in-iphone/
U follow the step by step to make the scroll view in iphone...good luck:)