Resize UIButtons from swift once constraints are set - ios

I was looking at this youtube video https://www.youtube.com/watch?v=vzyd33Pv9kg regarding How Priority Constrains works.
I want to do the same but instead of label I am using custom buttons with images. As you can see in the video, the author hardcoded the size of the label to 100wx100h. However regardless of the what iphone you have, the labels always be the same size. I want to accomplish the same thing, but instead I want the size of buttons (or in his case labels) be larger on iphone 5, and even larger in iphone 6 and so on.
My question is how can I accomplish this?
Also is it possible design this with let say 100X100 in 3.5-inch iphone then change the size dynamically change in the code depends what iphone is used?
if iphone4s then use 100X100 size buttons
else if iphone 5 then use 110X110 size buttons and so on?

Yes you can. Just connect your relevant constraints(width and height constraints in your case) to your view controller like usual elements. Than you can change constant property of your NSLayoutConstraints (Probably in your viewDidLoad method).
self.buttonWidthConstraint.constant = 110.;
self.buttonHeightConstraint.constant = 110.;
You also may want to change size classes to provide different constrains for each orientation.
https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/AboutAdaptiveSizeDesign.html

Related

How to have different layouts for iPhone and iPad (Swift)

In an app that I use, there are 4 rows of buttons with each row containing 3 buttons. When I use the same app on an iPad, the alignment of the buttons becomes 2 rows containing 6 buttons each. Also, the width and height of the buttons change depending on the screen size. I want my app to have the same features but I don't know how to do it. Any help?
1) You can use Autolayout constraints and size classes to make the view compatible to both iPhone and iPad screen sizes.
2) You can create and use separate Storyboards or .Xibs each for iPhone and iPad and use accordingly.
3) if the view does not have complex UI, then you can simply manage by doing it via code by detecting the view size, width height etc i.e whether it is iphone or iPad and then setting the frame, width, height of the UIElements like button, label, image view etc.
You need to use size classes. Following is the link which helps you to understand size classes :
Size Classes or Adaptive layout
Use Autolayout for defining constraints on buttons.
There might be two cases :
Either you want your buttons to be of the same height/width; In this case, you can define the height and width constraints to your buttons.
Or you want the buttons to be bigger. For that, you can define autolayout constraints in code if the screen size is for iPad.

Size classes and storyboard in UITableView

I need to add 3 labels to my UITableViewCell when my phone is in portrait, but when it's in landscape mode it should display 5 labels. Just to add complexcity when same cell is in ipad landscape it should show 6 labels. It should look something like this.
iPhone portrait
iPhone landscape
iPad landscape
I can do this using diffrent UITableViewCell XIB's but I want do this using Size classes and storyboard. Is there any example / tutorial / suggestion to follow ?
What you're looking for is this tutorial by Apple:
https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/EnableAndDisableViews.html
This panel will install a given view for a given size class, in other words, make sure it exists or doesn't exist:
(source: apple.com)
This still doesn't give you the opportunity to change it for iPad specifically because the size classes aren't distinguishable but perhaps you could smuggle as I think the iPhone has it's height as Compact in that mode and the iPad and iPhone 6(S) Plus as Regular. So you need to add that extra label in the width looking at the combination of Regular width and height.
Also make sure you install the right layout constraints in more or less the same way as in one scenario you space to the next label while in the next scenario that label is gone and you need to space it to the right side.

Using auto layout constraint priorities to change the size of a view on different devices

I am using autolayout to create a UI. I am setting some constraints there.
In the above image, as you can see there, is a image view in the center. Now I want to set the size of the image view not proportional on iPad. I want to set its size slightly bigger than the iPhone 5, and in the center of the view. I want to use priorities here. How can I achieve that?
Hey you can use size classes for such kind of work. In iPad apply different constraint as you need.Follow the below for complete reference to this:https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/AboutAdaptiveSizeDesign.html

How to handle iPhone 4s, 5 and 6 screen size, and make all the contents look the same across al the devices

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

Float layout in interface builder in iOS?

I want add one UIView – which has various number of UILabel objects – to a UIViewController.
How is it possible to set up constrains via auto-layout so, that if rotation occurs, and view width decrease, than height should increase, and labels should fit in like below, as CSS float do it.
Is it possible with interface builder and not programmatically?
UPDATE
Meantime I experimented iOS8 new interface builder feature called Size Classes (WWDC 2014 What's New in Interface Builder from 29:59), as matt has recommended down. But as I see, it supports something different, that I am looking for. Size Classes aim is to put different xibs (iPhone, iPad) into one xib, to have one xib instead of more.
In my case it does not help. What I have done to set up different auto-layout constrains for these two size classes:
But when I was running the application in iPad 4 simulator - what is the main target for my app - it used always the regular width / any height size class, and never the any width / any height, what my original expectation would have.
I do not know what "in a floating way" means. But in Xcode 6 you can easily do what you're describing, changing the constraints in an iPhone app so that the fourth label drops down to the next line, using the new conditional constraints (size classes) feature. With this feature, you can have one set of constraints for one set of size classes (e.g. iPhone in landscape) and another for a different set of size classes (e.g. iPhone in portrait).
Prior to Xcode 6, the answer would be no, you can't do that using IB alone. Code of some sort, whether a collection view or your own code responding to rotation/layout, would have to change the constraints.
With UICollectionView you can set up layout like CSS float.

Resources