UIWebView background image frame - ios

This is my code
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if ([[UIScreen mainScreen] bounds].size.height == 568) {
webview.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"tripadvisorwaiting5.png"]];
CGRect frame = webview.frame;
frame.size.width = 320;
frame.size.height = 568;
webview.frame = frame;
}
else {
webview.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"tripadvisorwaiting.png"]];
CGRect frame = webview.frame;
frame.size.width = 320;
frame.size.height = 480;
webview.frame = frame;
}
}
else {
webview.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"tripadvisorwaitingipad.png"]];
CGRect frame = webview.frame;
frame.size.width = 768;
frame.size.height = 1024;
webview.frame = frame;
}
but when the page loads, the image is too big or too small, and it's not scaled depending on the device.
Can you help me?

I believe +colorWithPatternImage sizes the image to the frame at the time you call it, so resizing the frame after calling webview.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"tripadvisorwaiting5.png"]]; would have no effect on scaling the image. Try setting the background image after you set the frame size. For instance:
if ([[UIScreen mainScreen] bounds].size.height == 568) {
CGRect frame = webview.frame;
frame.size.width = 320;
frame.size.height = 568;
webview.frame = frame;
webview.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"tripadvisorwaiting5.png"]];
}

Related

view frame does not move down 20px (height of the statusbar)

I used the code below to present vViewController1
#property (retain,nonatomic) ViewController1 *vViewController1;
...
push vViewController1 from rootViewController
[self.navigationController pushViewController:vViewController1 animated:NO];
Viewcontroller1's viewWillAppear
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
CGRect screen = [[UIScreen mainScreen] bounds];
if (self.navigationController) {
CGRect frame = self.navigationController.view.frame;
frame.origin.y = 20;
frame.size.height = screen.size.height - 20;
self.navigationController.view.frame = frame;
} else {
if ([self respondsToSelector: #selector(containerView)]) {
UIView *containerView = (UIView *)[self performSelector: #selector(containerView)];
CGRect frame = containerView.frame;
frame.origin.y = 20;
frame.size.height = screen.size.height - 20;
containerView.frame = frame;
} else {
CGRect frame = self.view.frame;
frame.origin.y = 20;
frame.size.height = screen.size.height - 20;
self.view.frame = frame;
}
}
}
}
but it displays as below and does not move down 20px(height of the statusbar)
Your comment welcome
Assuming you are NOT using Autolayout (seeing as you are playing with frame sizes) then in Storyboard select the ViewController and then goto the Size Inspector (little ruler) and then adjust the Y delta for the view (change to 20 or -20 depending on what mode you are "View As").
This may help here although I am not sure about the 18 value in Y I always thought it was 20!

URL is not loading completely on my UIWebView in iOS?

I am working on an iPhone application, where i am opening an URL to my UIWebView. The problem is URL is not loading perfectly. As per my requirement, i need to add UIWebView as footer view of my table. but the problem is after getting the web view content height size from webViewDidFinishLoad method, i am setting frame of my footer view. here is my code :
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
aWebView.scrollView.scrollEnabled = NO;
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
[self setFooterView:fittingSize.height + aWebView.frame.origin.y];
}
-(void)setFooterView:(float)fittingHeight
{
bottomView.frame = CGRectMake(0, 0, 320, fittingHeight);
webViewSocial.frame = bottomView.frame;
[tableView reloadData];
}
But when i am scrolling my table view, then my UIWebView (footer view) is not scrolling completely. Can you please tell me how can i achieve my output. Thanks!
try to set like this
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
CGFloat width = [[self.webView stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
CGRect frame = self.webView.frame;
frame.size.height = height;
frame.size.width = width;
self.webView.frame = frame;
self.tableView.tableFooterView = webView;
}

Set position of UIPickerView at bottom even after scroll in ios

I am trying to show UIPickerView on click on UITextField. I am using a simple method to show the picker as follows:
-(void) showPickerView {
[self.view resignFirstResponder];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.50];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if(iOSDeviceScreenSize.height == 480){
CGRect frame = self.picker.frame;
frame.origin.y = 270;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 226.0;
self.toolBar.frame = frame;
} else if(iOSDeviceScreenSize.height == 568){
CGRect frame = self.picker.frame;
frame.origin.y = 239.0;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 195.0;
self.toolBar.frame = frame;
}
[UIView commitAnimations];
}
It works great if I have not scrolled down. But when I scroll down, my picker is placed on fix position as mentioned in showPickerView method. Now how I can manage it that my picker appears at bottom every time.
Solution 1:
Add your pickerview to your window.
Solution 2:
Implement the scrollview delegate and adjust the frame continuously.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if(iOSDeviceScreenSize.height == 480){
CGRect frame = self.picker.frame;
frame.origin.y = 270;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 226.0;
self.toolBar.frame = frame;
} else if(iOSDeviceScreenSize.height == 568){
CGRect frame = self.picker.frame;
frame.origin.y = 239.0;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 195.0;
self.toolBar.frame = frame;
}
}
I can just suppose that you are showing your picker inside of a scroll view, right?
And if so, then your picker frame is relative to UIScrollView, not to a screen view.
So you have to take into account your scroll view offset. Something like that:
CGRect frame = self.picker.frame;
frame.origin.y = self.scrollvew.contentOffset.y + 270;
Otherwise you can show your picker outside of the UIScrollView (just add it to a screen view).

How to drawrect paintcode content centre horizontal on landscape iOS on retina iPad?

I'm getting in a bit of a muddle trying to draw some paintcode graphics code in the middle of the screen horizontally.
I'm really not sure if scale or landscape orientation affects this.
I've created a UIView subclass and addSubview in viewDidLoad like so...
MenuTitleView *fancyView = [[MenuTitleView alloc] initWithFrame:self.view.bounds];
[[self view] addSubview:fancyView];
Rather than the size PaintCode gave me...
CGRect textRect = CGRectMake(0, 0, 418, 129);
I'm trying to determine the screen / view width and the size of the canvas width.
I've tried various things without success.
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGFloat w = screenRect.size.width;
CGFloat width = 500;
CGFloat height = 200;
CGFloat x = (w - width) * 0.5f;
CGFloat y = 20;
CGRect textRect = CGRectMake(x, y, width, height);
Scale is not an issue, because Quartz and UIKit now work in 'points' rather than in pixels, so that's already accounted for. However orientation is not, [UIScreen mainScreen] will return the same rect either for applicationFrame or bounds etc, it's oblivious to orientation.
I like to put a solution in a category on UIView or UIViewController, because you'll reuse it plenty. something like this...
-(CGRect )workingCanvas{
CGRect screenRect = [UIScreen mainScreen].bounds;
BOOL landscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
CGRect result = CGRectZero;
CGFloat lesserDimension = (screenRect.size.width < screenRect.size.height) ? screenRect.size.width : screenRect.size.height;
CGFloat greaterDimension = (screenRect.size.width > screenRect.size.height) ? screenRect.size.width : screenRect.size.height;
result.size.width = (landscape) ? greaterDimension : lesserDimension;
result.size.height = (landscape) ? lesserDimension : greaterDimension;
if ([[UIApplication sharedApplication]isStatusBarVisible]){
result.size.height -= [[UIApplication sharedApplication]statusBarFrame].size.height;
}
return result;
}
if you want to position your view just in the middle of the screen try this
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGFloat width = 500;
CGFloat height = 200;
CGFloat x = CGRectGetMidX(screenRect);
CGFloat y = 20;
CGRect textRect = CGRectMake(x, y, width, height);
already tested is working
Good Luck!!

Height and width for landscape mode is showing wrong

I have used this method for detecting the width and height of screen. But its showing the width as 768 and height as 1024 in portrait and landscape also.
CGRect screenBounds = [[UIScreen mainScreen] bounds];
float widthfloat= screenBounds.size.width;
float heightfloat= screenBounds.size.height;
NSLog(#" width float %f",widthfloat);
NSLog(#"height float %f",heightfloat);
NSLog(#"width view %f \n height view %f", self.view.bounds.size.width, self.view.bounds.size.height);
NSLog(#"width %f \n height %f", screenBounds.size.width, screenBounds.size.height);
float wvalue = [[UIScreen mainScreen] bounds].size.width;
float hvalue = [[UIScreen mainScreen] bounds].size.height;
NSLog(#" wvalue %f",wvalue);
NSLog(#"hvalue %f",hvalue);
CGFloat width1 = [UIScreen mainScreen].bounds.size.width;
CGFloat height1 = [UIScreen mainScreen].bounds.size.height;
NSLog(#"width1 %f",width1);
NSLog(#"height1 %f",height1);
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGRect screenBounds1 = [[UIScreen mainScreen] bounds];
CGSize screenSize = CGSizeMake(screenBounds1.size.width * screenScale, screenBounds1.size.height * screenScale); if (screenSize.height==1136.000000)
NSLog(#"abcd1 %f",screenBounds1.size.width);
NSLog(#"abcd2 %f",screenBounds1.size.height);
NSLog(#"efgh1 %f",screenSize.width);
NSLog(#"efgh2 %f",screenSize.height);
bounds contains the bounding rectangle of the screen, measured in points. It does not care about screen orientation.
You can calculate the screen size, I do something like this for me -
-(CGSize)screenSize
{
CGSize size = [UIScreen mainScreen].applicationFrame.size;
if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
return size;
else
{
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
if (size.height > 480.0)
// This is iPhone-5
size = CGSizeMake(568.0, 320.0);
else
// This is iPhone-4
size = CGSizeMake(480.0, 320.0);
}
else
{
// This is iPad
size = CGSizeMake(1024.0, 768.0);
}
if (![[UIApplication sharedApplication] isStatusBarHidden])
size.height -= 20.0;
return size;
}
}
Note: The method will give you the height after reducing the height of the statusbar if available.

Resources