WKWebView show content wrong frame in old project - ios

I have an old iOS project using Objective C. When I add a WKWebView to a ViewController
NSURL *url = [NSURL URLWithString:#"https://www.google.com"];
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
_webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:theConfiguration];
[self.view addSubview:_webView];
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:#[
[self.webView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
[self.webView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
[self.webView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
[self.webView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor]
]];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
If I install app via Xcode with iOS 14, webview works fine, but after I disconnect Xcode and open app directly, webview show content with wrong frame. If I build app in iOS 12.5 via Xcode, web view also show content with wrong frame.
If I create new project from Xcode 12.5, and using same code, it works fine.
This is screenshot when open app via Xcode build
https://i.stack.imgur.com/rYb3M.png
And this is screenshot when open app directly:
https://i.stack.imgur.com/4q0uD.png

A few observations -
This code uses a mix of _webView and self.webView. For clarity, you should unify the usage for at least this code block.
Where exactly are you calling this part? viewDidLoad() or After some event? Can you try adding a layoutIfNeeded() call after you add this?
/// After activating constraints
[self.view layoutIfNeeded];
You should try to use leadingAnchor & trailingAnchor instead of leftAnchor & rightAnchor respectively.
If nothing helps, I think your view controller's view storyboard setup isn't updated to use Autolayout. In that case, you can try this -
_webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:theConfiguration];
_webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.view addSubview:_webView];
Please note that in this case - you MUST REMOVE all the constraints related code.
/*
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:#[ ...... ]];
*/

After spending a few days searching, I finally found the cause of the error.
In a UIView Extension file overridden center property
#import <UIKit/UIKit.h>
#interface UIView (FrameExtension)
#property CGPoint center;
#end
.
#import "UIView+FrameExtension.h"
#implementation UIView (FrameExtension)
- (CGPoint)center {
return CGPointMake(self.frame.origin.x + self.frame.size.width / 2,
self.frame.origin.y + self.frame.size.height / 2);
}
- (void)setCenter:(CGPoint)center {
CGRect frame = self.frame;
frame.origin = CGPointMake(center.x - self.frame.size.width / 2,
center.y - self.frame.size.height / 2);
self.frame = frame;
}
#end
Somehow, when loading the webview, it called setCenter which caused the error.

Related

I am getting a large black bar at the top and bottom of the screen in all simulator

this is code written in CDVViewController.m based on this only its showing content. Iam new to Cordova can anyone help me?
-(void)createView
{CGRect webViewBounds = self.view.bounds;
webViewBounds.origin = self.view.bounds.origin;
self.webView = [self newCordovaViewWithFrame:webViewBounds];
self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.view addSubview:self.webView];
[self.view sendSubviewToBack:self.webView];
}
-(UIWebView*)newCordovaViewWithFrame:(CGRect)bounds
{
return [[UIWebView alloc] initWithFrame:bounds];
}
Sounds like you did not include a launch storyboard (or launch images for all expected sizes). Add one of those to your project and you should be fine.

CGRectMake doesn't work

I'm working on example from "iOS Programming Cookbook", based on iOS 7. I need to create UITextField and add it to ViewController.
In ViewController.m I have:
- (void) viewDidLoad {
[super viewDidLoad];
[self createField];
}
- (void) createField {
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20.0f, 35.0f, 280.0f, 30.0f)];
self.textField.translatesAutoresizingMaskIntoConstraints = NO;
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.placeholder = #"Enter text to share";
self.textField.delegate = self;
[self.view addSubview:self.textField];
}
On screenshot from book textField appears in the middle of screen's width under the status bar, but when I run it textField appears on the top left corner of screen behind the status bar.
Maybe the problem is, that I run app on iPhone 6 simulator with iOS 8. But I don't know how to solve it.
Thanks!
Using self.textField.translatesAutoresizingMaskIntoConstraints = NO; is pretty much telling the object that it doesn't really care about the frame but relies more on the constraints that you give it. Setting it to YES takes the frame and automatically applies constraints to it to mimic the frame that you give it.
For example it would apply the constraints to have the frame appear at: CGRectMake(20.0f, 35.0f, 280.0f, 30.0f). When setting it to NO use NSLayoutConstraint and create the constraints programatically.
But in your case just remove the line
self.textField.translatesAutoresizingMaskIntoConstraints = NO;
because it is set to YES by default.

How to create a webview with only code

We are trying to create a simple app in xcode 5 targeting ios 7. We want to create a simple webview but we do not care to use the storyboard to set the whole thing up.
Is it possible to create a webview (and set the proportions/position) by only using a code snippet?
Thanks.
You just need to create a UIWebView instance and add it to a container view. You can defined the container view in code, a nib, or a storyboard.
CGRect frame = CGRectMake(0, 0, 100, 100); // Give the frame a position and size
UIWebView *webView = [UIWebView alloc] initWithFrame:frame];
[containerView addSubView:webView];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL urlWithString:#"http://www.google.com"]]];

UIWebview full screen size

I am trying to display a pdf in a UIWebview using the entire screen of the device, except the status bar and top bar. So far, I created IBOutlet UIWebview *webview; in my .h file. I connected the webview in my storyboard and wrote the following code in my .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
webview.scalesPageToFit = YES;
webview.autoresizesSubviews = YES;
webview.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[webview setBackgroundColor:[UIColor clearColor]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Chart" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webview loadRequest:request];
[self.view addSubview:webview];
}
The pdf displays correctly in my 3.5 inch, but in my 4 inch display, the bottom is cut off. This is even more noticeable when I rotate to landscape view. About 33% of the screen is not used at all. How can I fix this? I know it must have something to do with the UIWebview dimensions in my code above (webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];), but is there a better way to simply display the webview on the entire screen, without specifying a specific screen height and width?
One cheap thing to do would be to set the frame of the webview in overriding - (void)viewDidLayoutSubviews
- (void)viewDidLayoutSubviews {
webView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
the advantage of doing it here, is that the size and orientation of the view has already been determined here, and you can get an accurate measurement.
Using Auto Layout
Be sure the "Constrain to margins" option is not checked, all constrain values are 0:
And your webview is a first child of your main view:
Without Auto Layout
Change webview autosizing connections:
After a lot of headache dealing with compatibility of iOS6, 7 and different screen sizes, I use this one:
- (void)viewDidLoad {
[super viewDidLoad];
self.webview = [[UIWebView alloc] init];
// More setting your webview here
// Set webview to full screen
self.view = self.webview;
[self.webview loadRequest:aRequest];
}
I was able to get this working correctly for me by selecting the Reset to Suggested Constraints under the Resolve Auto Layout Issues menu.

scrollView with webView not scrolling horizontally

I'm trying to create an app to display various document formats and have the following code in a ViewController.m file:
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768 , 1024)];
[self.view addSubview:scrollView];
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768 , 1024)];
[scrollView addSubview:webView];
[webView loadRequest:service.urlRequest];
}
The trouble is, the app is only to be used in portrait mode and one of the Word documents is 1024 wide. I'd like to be able to scroll horizontally to see the whole document but this doesn't work.
I think I need to implement some setting on the scrollview but so far haven't been able to figure out what it is.
I've tried setting the width of both the scrollView and webView widths to 1024 and scrollView.contentSize.width but appear to be barking up the wrong tree.
I've also tired self.scrollView.contentSize = CGSizeMake(1024, 1024); but feel as though I'm just trying various things in an almost random manner to see if one works.
Can anyone tell me how to implement this?
WebView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

Resources