UIWebView content how to resize/scale it? - ios

I came to a point where I was able to lock the horizontal scroll as I wanted however when I rotate from landscape to portrait the content is still to large. I need to resize/scale it down myself but I don't know how to so far I use this :
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://igo.nl";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
_webView.scrollView.bounces = false;
_webView.scalesPageToFit = YES;
_webView.contentMode = UIViewContentModeScaleAspectFit;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if(fromInterfaceOrientation == UIInterfaceOrientationPortrait)
{
//_webView.scrollView.contentSize = CGSizeMake(320, _webView.scrollView.contentSize.height);
}
else
{
//[_webView reload];
_webView.bounds = CGRectMake(0, 0, 320,_webView.bounds.size.height);
_webView.frame = CGRectMake(0, 0, 320, _webView.frame.size.height);
_webView.scrollView.contentSize = CGSizeMake(320, _webView.scrollView.contentSize.height);
}
}
EDIT:
What I seem to need is that the content is zoomed out

Use the delegate methods of UIWebViewDelegate
#interface YOUR_CLASS : PARENT_CLASS <UIWebViewDelegate>
In viewDidLoad
...
[_webView setDelegate:self];
...
Call the delegate method
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[webView.scrollView setContentSize: CGSizeMake(webView.frame.size.width, webView.scrollView.contentSize.height)];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[_webView reload]; //Or use meta tag
[_webView.scrollView setContentSize: CGSizeMake([_webView.scrollView.contentSize.width, _webView.scrollView.contentSize.height)];
}
You have to add meta tag to your HTML file
<meta name="viewport" content="width=device-width" />
or simply reload the webView
[_webView reload];

try in
-(void)viewWillLayoutSubviews
{
------
}
calculate using
self.view.bounds.size.width,
self.view.bounds.size.height

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation==UIInterfaceOrientationPortrait && toInterfaceOrientation!=UIInterfaceOrientationPortraitUpsideDown )
{
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight)
{
}
}

Related

UIWebView not loading delegate

I have an apps written with Objective C. I can't load web page to UIWebView. UIWebViewDelegate not working. My app code ;
#import <UIKit/UIKit.h>
#interface Reklamlarim : UIView <UIWebViewDelegate>
- (void)webSayfasiYukle;
#end
I did it this way .m page. webViewDidFinishLoad and webView didFailLoadWithError: not working.
#import "Reklamlarim.h"
#implementation Reklamlarim
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self webSayfasiYukle];
}
return self;
}
- (void)webSayfasiYukle{
NSLog(#"webSayfasiYukle");
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
webView.delegate = self;
NSString *url=#"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self addSubview:webView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"webViewDidFinishLoad");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"webView didFailLoadWithError");
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)aRequest navigationType:(UIWebViewNavigationType)navigationType {
return YES;
}
I have, ios 8 device, xcode 6.3. I'm sorry for my bad english.
thanks ...
You need to check how you are initiating the Reklamlarim view because
- (id)initWithFrame:(CGRect)frame
is only calling when you are initiating from some where and call this init function it doesn't call automatically.. so move [self webSayfasiYukle]; to
-(void)awakeFromNib {
[self webSayfasiYukle];
}
like this and try.

WebView height not set based on its content

I know this question was posted so many times but still I can not change the height of UIWebView based on its content
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title=#"webview";
[_webView setDelegate:self];
NSString * string = #"<h1>This is HTML string</h1>";
[_webView loadHTMLString:string baseURL:nil];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(#"start loadinng.......");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(#"finish loading.......");
CGRect frame = _webView.frame;
frame.size.height = 1;
_webView.frame = frame;
CGSize fittingSize = [_webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
_webView.frame = frame;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"Failed to load with error :%#",[error debugDescription]);
}
can anybody help me how can resize my UIWebView height?
Here is your answer.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (webView.isLoading){
return;
}
CGSize contentSize = webView.scrollView.contentSize;
NSLog (#"height: %f",contentSize.height);
}
After gettting this height reframe your webView with new width and height and also reframe its parent view if there any.
How about this.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView sizeToFit];
webView.frame = CGRectMake(x,y,width,webView.frame.size.height);
}
I usually use these methods, to set UIWebview frame as it's content size:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
CGRect frame = webView.frame;
frame.size.height = 5.0f;
webView.frame = frame;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGSize mWebViewTextSize = [webView sizeThatFits:CGSizeMake(1.0f, 1.0f)]; // Pass about any size
CGRect mWebViewFrame = webView.frame;
mWebViewFrame.size.height = mWebViewTextSize.height;
webView.frame = mWebViewFrame;
//Disable bouncing in webview
for (id subview in webView.subviews)
{
if ([[subview class] isSubclassOfClass: [UIScrollView class]])
{
[subview setBounces:NO];
}
}
}
They are automatically called (if you set webviews delegate to this class), when WebView has finished loading it's content.
or
check this
raywenderlich-tutorial
This might helps you :)
First calculate height of web view for desired content.
NSString* lString = #"<h1>This is HTML string</h1>";
NSAttributedString* lAttributedText = [[NSAttributedString alloc] initWithData: [lString dataUsingEncoding:NSUTF8StringEncoding]
options:#{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: #(NSUTF8StringEncoding)
}
documentAttributes: nil
error:nil];
CGRect lBoundingRect = [lAttributedText boundingRectWithSize: CGSizeMake(_webView.bounds.size.width, CGFLOAT_MAX) options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context: nil];
then set it to _webView.frame
Now, load web view
[_webView setDelegate:self];
[_webView loadHTMLString: lString baseURL:nil];

How can I use progressView during URL is loading?

I want the progressView working while my URL is loading. What should I do for that?
here's my .m code:
-(void)openURL{
UIWebView *webView = [[UIWebView alloc] init];
[webView setFrame:CGRectMake(0, 0, 320, 460)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]];
[[self view] addSubview:webView];
}
- (IBAction)goto:(id)sender {
[self openURL];
}
Implement UIWebViewDelegate, specifically:
-(void)openURL{
UIWebView *webView = [[UIWebView alloc] init];
webView.delegate = self;
// ... rest of method as above ...
}
- webViewDidStartLoad:(UIWebView *)webView {
// Start progress.
}
- webViewDidFinishLoad:(UIWebView *)webView {
// Stop progress.
}
You could do something like this
[progressView startAnimating];
dispatch_async(dispatch_get_main_queue(), ^{
[webView loadRequest:yourRequest];
[progressView stopAnimating];
[progressView removeFromSuperView];
}
This will start the progress view before the operation begins, and will stop and remove it when it finishes.

Open web page in UIWebView after click on the button

I need help. I have two button, and I need each button to open a different web page in my UIWebView (for example: button1 open website apple.com, button2 open google.com). I can not find any tutorial that would help me. Thx for help (I use Storyboard).
There is my code, but something is wrong, because I see only a black screen (after click on the button)
TabulkaViewController.m
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(10, 240, 300, 35);
[myButton setTitle:#"Buttonone" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: myButton];
}
-(void) myButtonClick:(NSString *)myString
{
NSURL *url = [NSURL URLWithString:#"http://www.apple.com/"];
WebViewController *viewWeb = [[WebViewController alloc] initWithURL:url andTitle:#"Apple"];
[self presentViewController:viewWeb animated:YES completion:nil];
}
WebViewController.h
#import <UIKit/UIKit.h>
#interface WebViewController : UIViewController <UIWebViewDelegate>
{
NSURL *theURL;
NSString *theTitle;
IBOutlet UINavigationItem *webTitle;
}
- (id)initWithURL:(NSURL *)url;
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string;
#property (strong, nonatomic) IBOutlet UIWebView *viewWeb;
#end
WebViewController.m
#import "WebViewController.h"
#implementation WebViewController
- (void)viewDidLoad
{
[super viewDidLoad];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[_viewWeb loadRequest:requestObject];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string {
if( self = [super init] ) {
theURL = url;
theTitle = string;
}
return self;
}
-(id)initWithURL:(NSURL *)url {
return [self initWithURL:url andTitle:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
_viewWeb.delegate = nil;
[_viewWeb stopLoading];
}
#end
When working with storyboard, it would help if you upload your project file somewhere.
But from experience you need to set your webView delegate to your class WebViewController
you can do it by adding this in your viewDidLoad:
self.webView.delegate = self;
then you must implement the shouldStartLoadWithRequest delegate call, this is a good start.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
let me know if this fixes it.
EDIT:
after checking your project, change WebViewController->viewDidLoad to this:
- (void)viewDidLoad
{
[super viewDidLoad];
// init webview
_viewWeb = [[UIWebView alloc] initWithFrame:self.view.frame];
_viewWeb.delegate = self;
// add it to this controller's view
[self.view addSubview:_viewWeb];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[self.viewWeb loadRequest:requestObject];
}
your _viewWeb wasn't initialized as I suspected, See my comments on how to initialize it and how to add it to your WebViewController
EDIT2:
Added constraints in for the web view to take full screen, I recommend you read-up about auto layout in apple docs
- (void)viewDidLoad
{
[super viewDidLoad];
// init webview
_viewWeb = [[UIWebView alloc] initWithFrame:self.view.frame];
_viewWeb.delegate = self;
// add it to this controller's view
[self.view addSubview:_viewWeb];
// add constraints
NSDictionary* dict = #{#"viewWeb":_viewWeb};
_viewWeb.translatesAutoresizingMaskIntoConstraints = NO;
NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-0-[viewWeb]-0-|" options:0 metrics:nil views:dict];
[self.view addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-0-[viewWeb]-0-|" options:0 metrics:nil views:dict];
[self.view addConstraints:constraints];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[self.viewWeb loadRequest:requestObject];
}

How can I add a UIActivityMonitor to my interface?

I have a view controller that houses a UIWebView. I want to display an Activity indicator in the top right so the user knows the webpage is loading; however on build&run, the activity monitor does not show.
I have a UIWebView outlet called webView and a UIActivityIndicatorView outlet called activityIndicator.
Here is my implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
webView.delegate=self;
[self.view addSubview:activityIndicator];
NSString *fullURL = #"http://www.oncologyeducation.com"; NSURL *url = [NSURL URLWithString:fullURL]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
- (void)viewDidUnload
{
[self setWebView:nil];
[self setActivityIndicator:nil];
[self setActivityIndicator:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
return NO;
} else {
return YES;
}
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return YES;
}
-(void) webViewDidStartLoad:(UIWebView *)webView{
NSLog(#"load started");
[activityIndicator startAnimating];
activityIndicator.hidden = NO;
}
-(void) webViewDidFinishLoad:(UIWebView *)webView{
NSLog(#"load finished");
[activityIndicator stopAnimating];
activityIndicator.hidden = YES;
}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[activityIndicator stopAnimating];
activityIndicator.hidden = YES;
}
#end
On my storyboard, I have an activity indicator view which is connected to File's Owner. Thank you for any and all advice!
#CodaFi - you're right, that was not necessary. No need to add the subview.
Anybody who wants to reuse the code, lose the line:
[self.view addSubview:activityIndicator];
Turns out xCode was acting kind of wonky and copying the wrong storyboard into the project on compile. I renamed my storyboard, set it to the new name in the project settings, and the indicator spun with glee!

Resources