UIWebView loads page with 20 pixel gap in portrait - uiwebview

I have a UIWebView to load certain pages.In iOS 8 it loads page with 20 pixel gap from top specially in portrait mode,in landscape mode it is fine.How to handle the gap in portrait?

Place following code in your viewWillAppear :
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
CGRect viewBounds = [self.webView bounds];
viewBounds.origin.y = 20;
viewBounds.size.height = viewBounds.size.height - 20;
self.webView.frame = viewBounds;
}
Check this for more information : iOS 7 Status bar with Phonegap

Related

Get Navigation bar height in xamarin forms?

I am working on a project where i need to get the navigation bar height.I am getting the whole screen height using:
Helpers.ApplicationContext.ScreenHeight = (int)Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density;
Helpers.ApplicationContext.ScreenWidth = (int)Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density;
But I am not able to get the navigation bar height.
Can any one please suggest an idea to get the navigation bar height of my page.
Thanks in advance.
For iOS This may help you :
var navbarHeight = YourViewControllerInstance.NavigationController?.NavigationBar.Frame.Height;
For Android :
TypedArray styledAttributes = this.Theme.ObtainStyledAttributes(
new int[] { Android.Resource.Attribute.ActionBarSize });
var actionbarHeight = (int) styledAttributes.GetDimension(0, 0);
styledAttributes.recycle();
Above example gives Height in Pixel. Please find more refined sample as below which provide height in Pixel as well as DP:
TypedValue tv = new TypedValue();
int actionBarHeightInPixel = 0;
int actionBarHeightInDP = 0;
DisplayMetrics metrics = Resources.DisplayMetrics;
if (Theme.ResolveAttribute(Resource.Attribute.actionBarSize, tv, true))
{
actionBarHeightInPixel = TypedValue.ComplexToDimensionPixelSize(tv.Data, metrics);
actionBarHeightInDP = actionBarHeightInPixel / (int)metrics.Density;
}

Density of screen in iOS and Universal WIndows App

Hi I need to know the density of screen in windows 10/8/8.1 and iOS.
I got screen density in Android using DisplayMetrics but I find no such option/property available in UWP and iOS.
So Is there any property through which I can get screen density in UWP and IOS.
UPDATE - June 8th 2018
Xamarin has released Xamarin.Essentials!
Xamarin.Essentials provides developers with cross-platform APIs for their mobile applications.
Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all in C# leveraging Xamarin. Xamarin.Essentials provides a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.
https://github.com/xamarin/Essentials
You can now query the device's display information without having to manage all of the individual platform code pieces.
Device Display Information - https://learn.microsoft.com/en-us/xamarin/essentials/device-display
How-To:
Add a reference to Xamarin.Essentials in your class:
using Xamarin.Essentials;
The following information is exposed through the API:
// Get Metrics
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
// Orientation (Landscape, Portrait, Square, Unknown)
var orientation = mainDisplayInfo.Orientation;
// Rotation (0, 90, 180, 270)
var rotation = mainDisplayInfo.Rotation;
// Width (in pixels)
var width = mainDisplayInfo.Width;
// Height (in pixels)
var height = mainDisplayInfo.Height;
// Screen density
var density = mainDisplayInfo.Density;
Useful Links:
(Github) Xamarin.Essentials - https://github.com/xamarin/Essentials
(Blog) Xamarin.Essentials - https://blog.xamarin.com/xamarin-essentials-cross-platform-apis-mobile-apps/
(Docs) Getting Started with Essentials - https://learn.microsoft.com/en-us/xamarin/essentials/get-started
Original Post (Before Xamarin.Essentials existed)
Below, I'm storing the specific OS's screen details in static vars, located in the Xamarin Forms App class
These all appear to work in my testing.
(copy/pasted from my github page - https://github.com/mattregul/Xamarin_Screensize)
Android (MainActivity.cs) - Jump to Github Page
// Store off the device sizes, so we can access them within Xamarin Forms
// Screen Width = WidthPixels / Density
// Screen Height = HeightPixels / Density
App.DisplayScreenWidth = (double)Resources.DisplayMetrics.WidthPixels / (double)Resources.DisplayMetrics.Density;
App.DisplayScreenHeight = (double)Resources.DisplayMetrics.HeightPixels / (double)Resources.DisplayMetrics.Density;
App.DisplayScaleFactor = (double)Resources.DisplayMetrics.Density;
iOS (AppDelegate.cs) - Jump to Github Page
// Store off the device sizes, so we can access them within Xamarin Forms
App.DisplayScreenWidth = (double)UIScreen.MainScreen.Bounds.Width;
App.DisplayScreenHeight = (double)UIScreen.MainScreen.Bounds.Height;
App.DisplayScaleFactor = (double)UIScreen.MainScreen.Scale;
Note In all Windows examples screensize is the root project namespace
UWP (App.xaml.cs) - Jump to Github Page
// You decided which is best for you...
// Do you want Size of the App's View
// or
// Do you want the Display's resolution
// ######################################
// Size of App's view
screensize.App.DisplayScreenHeight = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Height;
screensize.App.DisplayScreenWidth = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Width;
// Size of screen's resolution
//screensize.App.DisplayScreenWidth = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels;
//screensize.App.DisplayScreenHeight = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;
// Pixels per View Pixel
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/design-and-ui-intro#effective-pixels-and-scaling
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/screen-sizes-and-breakpoints-for-responsive-design
screensize.App.DisplayScaleFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
Windows Phone 8.1 (App.xaml.cs) - Jump to Github Page
// Size of App's view
screensize.App.DisplayScreenHeight = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Height;
screensize.App.DisplayScreenWidth = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Width;
// Pixels per View Pixel
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/design-and-ui-intro#effective-pixels-and-scaling
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/screen-sizes-and-breakpoints-for-responsive-design
screensize.App.DisplayScaleFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
Windows 8.1 (App.xaml.cs) - Jump to Github Page
// Size of App's view
screensize.App.DisplayScreenHeight = Window.Current.Bounds.Height;
screensize.App.DisplayScreenWidth = Window.Current.Bounds.Width;
screensize.App.DisplayScaleFactor = 1; // No scaling here? If you find a scaling for Windows 8.1, please let me know :)
Xamarin Forms Page (App.cs) - Jump to Github Page
namespace screensize
{
public class App : Application
{
public static double DisplayScreenWidth = 0f;
public static double DisplayScreenHeight = 0f;
public static double DisplayScaleFactor = 0f;
public App()
{
string ScreenDetails = Device.OS.ToString() + " Device Screen Size:\n" +
$"Width: {DisplayScreenWidth}\n" +
$"Height: {DisplayScreenHeight}\n" +
$"Scale Factor: {DisplayScaleFactor}";
// The root page of your application
var content = new ContentPage
{
Title = "Xamarin_GetDeviceScreensize",
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
HorizontalTextAlignment = TextAlignment.Center,
FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
Text = ScreenDetails
}
}
}
};
MainPage = new NavigationPage(content);
}
}
}
This list lists the ios device dpi information. You can also use GBDeviceInfo thrid-party tool to get the screen density for IOS device like this [GBDeviceInfo deviceInfo].displayInfo.pixelsPerInch;
IOS:
iPhones
iPhone 1/3G/3GS 320 x 480 163 ppi
iPhone 4/4S 640 x 940 326 ppi
iPhone 5/5C/5S 640 x 1136 326 ppi
iPhone 6/6S 750 x 1334 326 ppi
iPhone 6 Plus/6S Plus 1080 x 1920 401 ppi
Without downsampling: 1242 x 2208 461 ppi
Except for the 6th generation (= 5th) iPod touch,
all the models are equal to their iPhone counterparts
iPads
iPad 1/2 768 x 1024 132 ppi
iPad 3/4/Air/Air 2 1536 x 2048 264 ppi
iPad Mini 1 768 x 1024 163 ppi
iPad Mini 2/3/4 1536 x 2048 326 ppi
iPad Pro 2737 x 2048 264 ppi
Apple Watches
Apple Watch 38 mm 272 x 340 326 ppi
Apple Watch 42 mm 312 x 390 326 ppi
I found API in the UWP to retrieve the device dpi. Windows.Graphics.Display.DisplayInformation. You can try it.

Positioning sprites inside scene

I would like when i create all my sprites to get centred in the screen, but that is not the case, i have a sprite when i set it's position to CGPointMake(self.size.width / 2, self.size.height / 2) it gets centred vertically but not horizontally, when i set it to CGPointMake(0, 0) it sets at the bottom of the screen with only half of it's body visible ( which is what you expect) but horizontally it doesn't get positioned right ( half his vertical body appears ) until i set it's X to 300.
How come the sprite's Y position gets set right but the X doesn't ?
scene.scaleMode = .AspectFill
and i haven't set any anchorPoint, 4' screen.
UPDATE:
I'm using Portrait, played around with different scaleMode and found out that the perfect (0, 0) that sets the sprite on the bottom left is:
scene.scaleMode = .ResizeFill
It also works fine with different screen sizes,but (self.size.width / 2) puts it further to the left.Do you think it's the best setting for all SpriteKit Portrait projects?
I tried to reproduce what you are saying, but I couldn't, either using .sks file or not for loading the scene. Also I've produced good results on both 7.1 and 8.1 simulators...The thing is that simulators sometimes could be tricky and not 100% reliable, so you should not trust them much.
I don't know if this will help you, but you should consider it...In Xcode 5 projects scene is programmatically created to have a size of the view. Basically it's done in viewDidLoad method like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
GameScene * scene = [GameScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
Or using viewWillLayoutSubviews like LearnCocos2D pointed:
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
skView.showsDrawCount = YES;
//skView.showsQuadCount = YES;
skView.showsPhysics = YES;
skView.ignoresSiblingOrder = YES;
if(!skView.scene){
// Create and configure the scene.
GameScene * scene = [GameScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
}
But Xcode 6 uses an SKS file:
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
}
}
If you look at the GameScene.sks you can see that the default scene size is 1024x768.
Try to print your scene size as well as view's bounds size to see actuall sizes. You can use something like:
println("view.bounds\(view.bounds), self.size\(self.size)")
I don't know if any of this helped, but I hope it will lead you somewhere. Goodluck!

UIWebView keeps Portrait dimensions when viewed in Landscape

I have been unable to find a solution to this problem anywhere.
I am using Xcode 4.5 to write an iOS 6 app using storyboards. It uses a Navigation Controller to jump between several View Controllers with Navigation Bars, including a few that use a UIWebView to display a website. I have added code to the AppDelegate and various ViewController files to restrict the view to Portrait for most scenes in the app (since they really won't look good otherwise), but to allow Landscape view for the websites. And the Info.plist is configured allow all 4 orientations.
Everything works fine in the UIWebView scene; including rotation, scrolling and zooming - except for one thing: when I rotate from Portrait to Landscape, the horizontal dimension of the website stays locked at the horizontal dimension for the Portrait view - resulting in a large white space to the right of the website in the scene (see images below). I fixed a small (1/4") white space at the top of the UIWebView by selecting Layout: Wants Full Screen under View Controller in the Attributes Inspector. But I can't fix this.
I can supply relevant code or Storyboard settings if necessary. Can anyone help? Thank you.
I solved adding this code, when application loads on viewDidLoad method:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
Then add this to your view controller:
- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
_displayWeb.frame = CGRectMake (0, 0, 768, 1024);
break;
case UIDeviceOrientationPortraitUpsideDown:
_displayWeb.frame = CGRectMake (0, 0, 768, 1024);
break;
case UIDeviceOrientationLandscapeLeft:
_displayWeb.frame = CGRectMake (0, 0, 1024, 768);
break;
case UIDeviceOrientationLandscapeRight:
_displayWeb.frame = CGRectMake (0, 0, 1024, 768);
break;
default:
break;
};
}
_displayWeb is your IBOutlet UIWebView, by the way this is using Xcode 4.6.1

how can I set contentoffset and change the height of UIScrollview at same time

I'm trying to scroll up my UISCrollView when the keyboard is shown
I use setContentOffset to shift the uiview up.
At the same time I want to decrease the height of my UISCrollView to (view height - keyboard height), so that the entire content view can be scrolled.
I apply both the changes in keyboardWillShow notification
When I actually bring the keyboard up in my app, the contents first get pushed up and then they are pushed down (which gives a flickering effect). I'm trying to smooth out both the transformations in one go..
Is it possible?
Code Below ---
- (void) keyboardWillShow {
CGPoint contentOffset = scrollView.contentOffset;
CGRect scrollViewFrame = scrollView.frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
if (contentOffset.y >= 0 && contentOffset.x >= 0) {
isContentOffset = YES;
contentOffset.y += screenShift;
[scrollView setContentOffset:contentOffset animated: NO];
}
scrollViewFrame.size.height = keyboardOrigin.y - self.view.frame.origin.y - toolbarHeight;
scrollView.frame = scrollViewFrame;
[UIView commitAnimations];
}
There is an option for animation when you setContentOffset for animation. here is the code that i use all the time
- (void)textViewDidBeginEditing:(UITextView *)textView
{
svos = scrollView.contentOffset;
CGRect rect = [textView bounds];
rect = [textView convertRect:rect toView:self.scrollView];
CGPoint point = rect.origin ;
point.x = 0 ;
[self.scrollView setContentOffset:point animated:YES];
doneButton.enabled = YES;
}
- (IBAction)donePressed
{
[scrollView setContentOffset:svos animated:YES];
[textview resignFirstResponder];
doneButton.enabled = NO;
}
It works fine for me.
Are you wrapping these changes in an animation block?
[UIView beginAnimations:#"resizeScroll" context:nil];
// make your changes to set and content offset
[UIView commitAnimations];
I think I have got a solution for this. You can handle the resize of the view in textViewDidBeginEditing method. You can just change the frame size of the scrollView to half by
CGRect tframe = myscrollView.frame;
tframe.size.height/=2;
myscrollView.frame = tframe;
and to initialize or handle the total length of the scrollview you can set the contentSize of the scrollView in the similar fashion as the frame was set in above snippet. for example
CGSize tsize = self.view.frame.size;
//here you can change the height and width of the scrollView
myscrollView.contentSize = tsize;
I hope this should do the trick for you. If it does please communicate.
Figured out the issue...
When the keyboard button was clicked, I was doing a becomeFirstResponder followed by a resignFirstResponder on keyboard view. This caused keyboardWillShow followed by keyboardWillHide and another keyboardWillShow notification, which brought the keyboard up, brought it back down and then up again.
Thanks to everybody who tried to help out.. and sorry the issue was pretty different...

Resources