I have a very hard time to display UI properly on iPhone X, based on the screenshot, UI on iPhone X always get longer than normal iphones, is there any solutions on how to scale and display the ratio properly across all iPhone sizes? Thanks.
make imageView.width proportional to imageview.height and not to screen height.
i think in this case height-to-width will be in 4:3 ratio
Also calulate youe imageview height based on screenheight with safe area excluded from it.
if #available(iOS 11.0, *) {
if ((UIApplication.shared.keyWindow?.safeAreaInsets.top)! > CGFloat(0.0)) {
let safeArealength = UIApplication.shared.keyWindow?.safeAreaInsets.top + UIApplication.shared.keyWindow?.safeAreaInsets.bottom
height = ratio * (UIScreen.main.bounds.size.height - safeArealength)
width = 0.75 * height //guess the ratio is 4:3
}
}
here the ratio is the proportion between the imageview size and the screensize.
Related
I am working on an ObjectiveC app in which I need to set the width and height of a uiview in pixels and than scale that uiview equal to the devices width. The width and height values in pixels are fixed ( let say 900 px x 500 px).
Currently I am doing this,
UIScreen* mainScreen = [UIScreen mainScreen];
CGRect frame = CGRectMake(0, 0, 900.0, 500.0);
[view setFrame:frame];
CGFloat valScale = mainScreen.bounds.size.width/900.0;
[view setContentScaleFactor:valScale];
But this is not giving me the desired values.
What should I do?
(P.S) Does view.frame.size.width return the width in pixels or points?
Hi for scaling a view you can use the transform property. Say you wish to scale your UIView by a factor of 2 then you can use.
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2);
CGRect width and height returns points not pixels. One point has two pixels for 2x devices like iPhone 6,7 and three pixels for 3x devices like iPhone 6 Plus,7 Plus.
For more detail you can refer this Apple's documentation link.
How can I resize an image based on the height of the mobile device that's in landscape mode? I have a wide image (a ruler) and want to be able to slide the image back and forth. I've tried scaling the image, but I can't seem to get it to work.
func resizeImage(size: CGSize) {
let scaleFactor = imageView.bounds.height / size.height
let newHeight = imageView.bounds.height * scaleFactor
let newWidth = imageView.bounds.width * scaleFactor
var newSize: CGSize
newSize = CGSize(newWidth, newHeight)
imageView.frame = CGRect(origin: imageView.frame.origin, size: newSize)
scrollView.contentSize = imageView.bounds.size
scrollView.autoresizingMask = [.flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin, .flexibleRightMargin]
self.scrollView.contentMode = UIViewContentMode.scaleAspectFit;
}
If you are using storyboard then it is easier to do with auto layout constraints. You have to use Equal Height Constraint with the Multiplier. Although I am also a beginner and haven't used this method for landscape mode, but I am assuming it ll work for landscape too.
Follow these steps :-
Suppose the view you are using in your storyboard while creating the view is of iPhone 7. So your screen height will 667.
Now, suppose in this view your image looks perfect with height 200.
So take the the ratio 200/667=0.299
Now set Equal Height Constraint by selecting both views (i.e imageView and superView) using the ratio as multiplier.
You can check for other devices and orientations with an option on bottom View as:
In the end, height constraint of image should look like this. My height ratio is 0.4 (pic here)
I need to set an imageview's width (CGRect) and a NSLayoutconstraint, and then compare that value to the device's screen size. How do I do this, being that they are not of the same type?
For all ye who are in the same boat I was:
constraint.constant + UIImageview.bounds.width > screenSize.width
constraint is the NSLayoutconstraint, then the image width, and then the screen size. Be forewarned: screenSize.width seems to be measured in pixels.
What I want is if I design for iPad retina 1024 x 768 when someone use the app on iPad Pro or smaller size iPad all the elements should resize and fit the screen size. I don't want my layout to look smaller on bigger screen size or bigger on smaller screen size. How do I achieve it?
What I'm doing right now is
func getWidthfromConstantForScreenSize(constant: CGFloat, size: CGFloat = 1024, cSize: CGFloat = screenSize.width) -> CGFloat {
return cSize * (constant / size) // It returns the new size for the screen
}
func getHeightfromConstantForScreenSize(constant: CGFloat, size: CGFloat = 768, cSize: CGFloat = screenSize.height) -> CGFloat {
return cSize * (constant / size) // It returns the new size for the screen
}
But it's a big task for doing for every element any one know's how to achieve it easily?
I'm using autolayout but i still have to resize programmatically.
How to get the screen width and height only in landscape orientation , i have two ipad 4 tablets and on one of them it takes me the landscape width & height , and on the other one it takes me the portrait one even if it's orientation is on lanscape
at the moment i am using this but its not working well
CGFloat width = self.view.bounds.size.width;
CGFloat height = self.view.bounds.size.height;
Use [UIScreen mainScreen].bounds you'll have the same.
Discussion
This rectangle is specified in the current coordinate
space, which takes into account any interface rotations in effect for
the device. Therefore, the value of this property may change when the
device rotates between portrait and landscape orientations.
Use [UIScreen mainScreen].nativeBounds in iOS8 only to get the portrait-locked bounds.
Discussion This rectangle is based on the device in a
portrait-up orientation. This value does not change as the device
rotates.
Swift 3
let pixelWidth = UIScreen.main.nativeBounds.width
let pixelHeight = UIScreen.main.nativeBounds.height
let pointWidth = pixelWidth / UIScreen.main.nativeScale
let pointHeight = pixelHeight / UIScreen.main.nativeScale
print ("Pixels: \(pixelWidth) x \(pixelHeight)")
print ("Points: \(pointWidth) x \(pointHeight)")
On a 6s Plus will print...
Pixels: 1080.0 x 1920.0
Points: 414.0 x 736.0