Trying to achieve UIStackView with 2 labels for iPhone and 3 labels for iPad using size classes.
View hierarchy
Default size class
[UIStackView]
[UILabel 1] [UILabel2]
[UIStackView]
RxR size class
[UIStackView]
[UILabel 1] [UILabel2] [UILabel3]
[UIStackView]
It looks exactly as expected in Storyboard as you can see in the above screenshots but on simulator iPad view is messed up.
iPhone Output (okay)
iPad Portrait Output (messed up)
iPad Landscape Output (messed up)
What's happening in iPad?? Is it a bug or I'm missing something?
Thanks.
I have created a sample project.
Follow below steps to achieve your requirements :
Drag one horizontal Stackview in to your interface builder
set it's constraints like : top,leading, =trailing and fixed height
Drag and drop three labels in to it and select all together and set one constraint : Equal width
now select second or middle label and from attribute inspector click on + button beside installed and select Regular for both width and height. It will add regular,regular variation and uncheck the checkbox for that R,R variation. Refer below screen shot for better understanding,
And you are done! now you your center label will not show in iPad and every label will be displayed in iPhone!
See below result screenshot of iphone and ipad in my case!
iPhone :
iPad :
Update :
If you want two to hide label in iPhone and show in iPad then add one more variation : Compact Width Regular Height for middle label and uncheck the checkbox to uninstall. Refer below screenshot,
It seems you are hiding one of the labels on iPhone using the "installed" checkbox in Interface Builder. Using the "hidden" checkbox instead seems to solve the problem. Which I guess is a bug.
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
I want the user to be able to select a single row in a UITableView and then have that row show the Checkmark accessory. First to check how it looks in the UI, I added the checkmark through the XCode Interface Designer, like this;
Ran it on the iPhone X Simulator and it works as expected.
But when I run it on the iPad Pro (12.9 inch) (2nd generation) Simulator it doesn't show up.
So I thought that maybe the constraints were the issue, so I tried writing the widths of all the views that could be the problem to the console and they all returned 1024 so that is correct.
When I try setting the checkmark through code I have the same problem.
Am I missing something here?
EDIT:
Screenshot with red borders on the cells.
Your issue is definitely about constraints and Auto Layout.
You should set constraints to superView for checkmark : trailing/top/bottom
Horizontal spacing between the UILabel and the Checkmark.
And Edit this last constraint to make it "Greater or equal" so that it will eventually grow with bigger screen.
You will also want to change your content hugging priority.
Simpler solution : You could also use UIStackView in order to proportionally align your label and your checkmark regardless of the screen resolution.
I am using auto resizing xib in my application.
I disabled size class and auto layout.
this is how i set xib.
And this is how its looking on iphone 6s plus
How can i fit xib to all device without using auto layout and size class?
actually you set the layout for every label in left corner, thats the reason its showing full left.
for your First label Size inspector is correct, if you need the modification do like
for your second Label do like
for your third and Final label Set like
Update
You can set label positions & width like see images:
Label 1:
Label 2:
Label 3:
Note: this will adjust the uilabels width & position according to the diff screen size not height.
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'm using Interface Builder in Xcode 6 to make an app and am having trouble getting the text fields and button to centre on the screen for different size screens.
I thought it was a matter of selecting horizontal and vertical centering in container but it doesn't seem to be that when I try it in auto layout. Actually I've tinkered around a bit and I still haven't got it.
I just want to be able to see all of my button and text fields for any size iPhone screen and right now simulator is only showing part of these UI elements like this:
I also want to do this in storyboard and not in code as I'm not at the level of doing this in code yet.
Step 1: Make sure your size class covers all the iPhone screen at least in portrait view. So, change the size class to "wCompact hRegular".
Step 2: After setting the size class properly, add the UITextFields and UIButton to your storyboard. To me, it looks something like-
Step 3:
Before, you start adding constraints, you need to remember two things-
a. Your element(UITextField, UIButton, UIView or any component) needs to know its starting position unambiguously, and
b. Your element needs to know its size meaning, its height and width.
In this case, as you want to centre your elements, I am just assuming that it needs to be centred starting from 10 scale from the left edge and should end 10 scales away from the right edge of your iPhone screen. Now, that means, it's width will be different based on the screen size, but its height will be same.
So, I just add the constrains following way for the 1st text box-
Notice, in the size inspector, I set the text box's starting point, x and width in a way that it is 10 pt away from left edge and 10 pt away from the right edge. Don't worry, it's just simple math.
For the 2nd textField, I add the constrain, the same way-
Lastly, for the button, the constrains are following-
Now, you are good to go. Everything is centered.
By using your size class selector in the bottom of the storyboard window, set you sizes as any width and any height and then follow the below auto layout constrains. It will work for you.
First select the view you want to set the auto layout, and then select the pin option from the right bottom corner of your storyboard and then add the constrains as shown in above picture and click button Add 4 constrains
Repeat the process for all views and set the constrains as Fix the top, bottom, left and right constrains of all views except the last button that should be fix from top,left,right and fixed height.
You need to make use of the size class selector in the bottom of the storyboard window.
So for an iPhone 6 or 6 Plus in portrait you would choose a compact width and regular height like this:
And then you would do whatever auto layout stuff for the given device there