i'm creating an app for iPhone and iPad. and now i would want to retrieve the size of an UIImageView on different phone screen. I have tried with following code:
print("width \(BubbleBackground.frame.size.width)")
print("height \(BubbleBackground.frame.size.height)")
print("width1 \(BubbleBackground.bounds.size.width)")
print("height1 \(BubbleBackground.bounds.size.height)")
print("width2\(BubbleBackground.image!.size.width)")
print("height2\(BubbleBackground.image!.size.height)")
I have tried with the code above, but yet they return me a constant value no matter it is running in iPhone 5, iPhone 6, iPhone 6 plus or iPad. Does anyone have an idea how can i get the image size when it is running on different device?
You have to have some constraints to change the imageView frame according to the screen size. Then you can use .layoutIfNeeded() on the UIImageView (BubbleBackground.layoutIfNeeded()) to get its runtime size.
make sure you do this in viewWillAppear or viewDidLayoutSubviews or anytime after these events.
Your image will be the same size on all devices unless you have set constraints on the image that cause it to resize to fit the current screen size.
Constraints are most often added using Xcode's Interface Builder but can also be added programatically.
Adding constraints using the Interface Builder
https://developer.apple.com/library/prerelease/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraintsinInterfaceBuidler.html#//apple_ref/doc/uid/TP40010853-CH10-SW1
Adding Constraints Programatically https://developer.apple.com/library/prerelease/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html
Related
When I run my app in simulator, the height and weight still get for my storyboard preview choose phone.
Should not be choose which kind of simulator?
ex:
storyboard preview choose iPhoneSE then the image view
weight = 96
height = 92
storyboard preview choose iPhone8 then the image view
weight = 112.5
height = 108.5
but when I storyboard preview choose iPhoneSE and simulator no matter choose which like iPhone 8, iPhoneX...
weight = 96
height = 92
Why?
Most weird thing is the same simulator when I choose different storyboard preview, I get different size...
print(self.img.frame.height)
print(self.img.frame.width)
I had this exact same issue, and I do not think it comes across well in the description above. To recreate it, take the following steps:
Setup:
1) Start a new 'Single View App' in Xcode. I am currently using Xcode 10.2.1
2) Go into Storyboards and add a UIView object with some sort of backgroundColor so that it is visible
3) Give this new view some auto contraints, such as a leading, trailing, centerY, and an aspect ratio of 1:1. Just make sure to not give it a fixed height or width.
4) In ViewController.swift, add an IBOutlet variable such as 'myView' for this UIView and connect it so that it is easy to reference.
5) In viewDidLoad(), add a print statement to the effect of,
print("Bounds of myView: \(myView.bounds)")
Testing the issue:
Now, back in Interface Builder, set the preview, or 'View as' at the bottom to something like 'iPhone SE'. Then, at the top, where you choose the simulator, also choose 'iPhone SE'. Now run the app.
In the console, you will see your print statement with the bounds of the view, and everything will look fine visually.
Now, change the Interface Builder's preview 'View as' option to something like 'iPhone 8', but keep the simulator option on iPhone SE
Again, run the app in the 'iPhone SE' simulator.
Just like before, visually everything is okay thanks to autolayout. However, the bounds information printed in the console is different. This instead matches the 'iPhone 8', or whatever device was chosen for preview in Interface Builder.
For an even more obvious example, choose something like an iPad Pro for the Interface Builder, and again run it on iPhone SE simulator. Again visually everything is okay, but the bounds information in the console is way off. It is tied to the Interface Preview and not to the device being tested.
The problems arise when you are writing code which uses the bounds information of an object under autolayout constraints which do not have a fixed width and height.
Solution:
In ViewController.swift, add the following after the viewDidLoad() method
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("Bounds of myView in viewDidLayoutSubviews: \(myView.bounds)")
}
Now, no matter what preview option we use in Interface Builder, the simulator logs the correct bounds information at this point in the lifecycle.
So, for any projects which make use of the bounds or frame property in code for such a view as the one in this example, should place any code refrencing that bounds within the viewDidLayoutSubviews() method.
if you added the leading, trailing, top and bottom constraints you will get different size of the imageView on different screen sizes and if you want the imageView to have a fixed size on every screen then you can use centerX and centerY constraints for positioning and add constant width and height
Example 1
Example 2
adding imageView width and height
adding centerX and centerY constraints
Explanation
in Example 1 image width and height will be dynamic and in Example 2 height and width of the imageView will be same no matter which device is used to display
in storyboard I set view controller size iPhone3.5-inch
add constraints with UILabel
UILabel.text cannot show all,so i change view controller size iPhone 4.7-inch
so i tap UIlabel update frame , autolayout work, it all ok!this is iPhone 4.7-inch
now the problem coming,when i run in iPhone simulator iPhone 6, autolayout don't working, the label size only zoom, the width don't change longer。 and The following three images constraints width and height both 32,but in simulator iPhone 6 images size Look bigger,Why is this so? please help me ,very thinks
No matter iphone6 or iphone5 iphon6+ ,code here output 320, why...
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
NSLog(#"%f",self.view.frame.size.width);
}
As per your question if you want text should not display ... then all you need to do is set number of lines = 0 and set Line break mode to word wrap.
As you are working with label it does not require any height constraint.
If you are building your application with size class and using launchScreen.xib then you label won't zoom and if you are using launch image then you need to all splash screen image of exact size.
You can find out about size of splash screen in below url.
https://stackoverflow.com/a/26191308/4101371
I've added images of view with constraint and changes in properties of label.
result will be displayed similarly in device and simulator.
And if you don't want to display label in multiple lines then you can set Auto Shrink (Minimum font Size/ Minimum Font Scale)
Hope it will help you in solving your problem.
Don't you forget to set constraints for your arrow image?
Set trailing and top constraint with fixed height/width.
Set 'Autoshrink' property of your label to 'Fixed font size'
this prevents label from changing font size depend on it's frame
Recreate your constraint for images - it looks like it pins to superview, so in runtime it scales to the size of superview
Hope this helps.
I have a Swift app which sets a UIImageView to the size of a portion of the screen (set with autoconstraints to the margins and controls above it), and I use the height and width values of the ImageView to create 3 additional UIImageViews programmatically and position them on top of the original image. I am constructing a podium and place three images on top of the podium in appropriate positions. I then calculate the height and width of each additional image by creating a constant: let conImageHeight = 0.4 * self.imgPodium.frame.height. However, I can't get a reliable height and width of the original image.
Initially I used self.imgPodium.bounds.height which worked great for the iPhone 6 simulator screen size, but when I tried running the app on an iPad simulator, it kept the iPhone size values. The image itself changed size but when I println() the height and width using bounds, I get the values of the iPhone image.
I then changed and used self.imgPodium.frame.height but this didn't change anything.
Is there a better way to get the height and width of a UIImageView (or any control for that matter) after it positions itself through autoconstraints?
Thanks.
Questions I've looked at already but didn't help:
Change UIImageView size and position programmatically in Swift
how to check screen size of iphone 4 and iphone 5 programmatically in swift
UIImageView Bounds, Frame
Cocoa: What's the difference between the frame and the bounds?
How to resize an UIImageView (swift)
In viewDidLoad the subviews have been loaded and initialized but not layed out yet. Therefore their frame property for example is not valid yet and may be the one that was set in Xcode when designing the UI in the first place - that however is not reliable. You should do custom layout in places where you can be sure the layout has already happened yet: e.g in viewWillLayoutSubviews or viewDidLayoutSubviews. Both methods will also get called when the layout somehow needs to change. In viewDidAppear the layout has been done yet and therefore you can do some layouting too. This code however will get not get called when the layout changes due to interface rotation for example. AND it will get called after a presented viewController gets dismissed again. (not what you would want).
In both cases: do your setup of creating new imageViews in viewDidLoad and do the layout in viewWillLayoutSubviews or viewDidLayoutSubviews!
I am using Xcode 6.1 and in one of the views I have, contains many labels. However, these labels look different in each different devices. Although I have add constraints to all the labels, but still facing the same issue.
How can I get the same view layout across all devices?
How can I make the width and height for each label is flexible, depends on the screen size?
Do I need to design different xib for each device?
The following images showing the result am getting, with auto resize enabled, and constants been added.
on iPhone 6
on iPhone 5
on iPhone iOS 7.1
We have provided with a simple solution . Using Aspect ratio constraint we can get a relative look in all device screens.
I had the same issue and I found this >>
http://mathewsanders.com/designing-adaptive-layouts-for-iphone-6-plus/
So when designing for lowest to highest screen resolution we can't set a specific width and height for all controls as it may look weird. So using this constraint we can have a relative look for all screens.
If you don't want to use constraints or you want to keep the same width & height , you can add scrollview to hold all controls. For that you can set a resizing frame for scrollview with fixed content size .
To solve this...
1) Go to xib file and uncheck the Auto layout
2) Then set the autosizing manually in the following manner for each and every subview.
Use self.view.bounds.size.width to get the width of screen, then use relative value to initialize all labels.
One soultion is that you can Design a story board for each screen size like one for iphone 5, one for iphone 6 and one for iphone 6+ and in your AppDelegate method "didFinishLaunchingWithOptions" you can get the screen size and popup corresponding storyboard
in this wasy you can set different constraints according to different screen size
I have a scroll view in a storyboard and the app is to run on iPhone4s and iPhone5s with iOS5 and iOS6.
I initially created the storyboard and was running on an iPhone5 and now I'm testing with an iPhone4.
Isn't there a way of just using a single storyboard for both the iPhone4 and iPhone5 and the when views etc. will automatically resize themselves depending upon the device, or do I have to use two separate storyboards?
I've tried numerous things, such as changing the simulated metrics size and many others but I can't find anyway of the view changing its size automatically - as I created the storyboard initially for an iPhone 5 the view now seems stuck at a 548 height when loaded. SUrely there must be a way of specifying in the storyboard that its height comes from the device? (Its height is the full screen less the status screen).
Further details
There is UIScrollView inside a UIView, the UIView will resize, the UIScrollView will not:
Yes, you can do it in the "Size Inspector", inside the Utilities menu. There you can set the autosizing and origin of any visible element.
This works in iOS 5 and 6 and it's not required to use the Autolayout, which is only for iOS 6.
Size inspector:
Second screenshot:
You have to use two storyboards for iphone4 and iphone5. Then you can check the device size to load the .xib according to it.