iOS grid UI control - ios

In XCode Swift Storyboard with auto layout enabled I need to put a row of buttons (let it be 6 buttons) filling screen by width. The buttons must be of the same width, keeping aspect ration the same (so row height will change when screen width changes).
I think that I need to use an UI control like GridLayout in Android, but so far I can't find anything suitable in iOS.
Please advice.

There's nothing in Interface Builder that allows you to do that automatically, that is, without having to position and set constraints for each your buttons yourself. For iOS 9.0, UIStackView might be helpful for you, but until then (and possibly even still) I'd do it programmatically using UICollectionView.

Related

iOS: Is it possible for UI objects to respect safe area without auto-layout in storyboard?

In my example, I have three buttons at the top of the UIStoryBoard. This works well for non-iphone x devices. However, it gets cut off when I use iPhone x. The buttons go above the safe area.
I was wondering if it is possible for the UIButtons to respect the safe areas without auto layout in storyboard? If not, how do I replicate this layout (UIButtons auto-resizing with flexible width and flexible height) with auto layout and respecting safe areas? I believe I should use aspect ratio on the UIButton's itself. Not sure if this is the proper way.
Turns out the answer is easier than I thought. Just put all the UIButton's inside another view. Then add constraints to that view that respects the safe areas. This way the UIbuttons can still use autoresizing.
Migrating a project using autoresizing masks for iPhone X

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.

How to resize my app for every iPhone

I'm a beginner app developer and just finished creating my first soundboard app, the only problem is that the layout of the app fits on an iPhone 6s but when i change the simulator to any other iPhone the app doesn't fit on the screen the positioning of it changes and some buttons are left out, how do i make the size of the app fit on every iPhone?
because you did not write code related to layout fitting.
there will be two ways you can auto layout your UI.
firstly,calculate the frame of each UI component and put those code into layOutSubviews.
secondly,using autoLayout is wise choice and I would recommend you Masonry which is a third-party autoLayout framework if you develop under Objective-C.
best regards & thanks
There are several ways to to accomplish this task:-
Auto Sizing, Old way to make UI for different screen size this
will use the struct and spring framework to create UI.
Multiple StoryBoard, You can create a separate storyboard for
different device. Simple but never recommend to use this way as it
will make application more complex and make application heavy, you
have to update every storyboard when ever you have to make changes
in UI
Programmatically, you can also create your UI
programmatically and calculate there frame and set constraint to
your UI
Auto layout "Recommended" , can create a single
storyboard and and apply auto layout constraint to you view on
storyboard and programmatically.
here are some link on auto layout
https://www.raywenderlich.com/115440/auto-layout-tutorial-in-ios-9-part-1-getting-started-2
http://www.appcoda.com/introduction-auto-layout/
https://www.weheartswift.com/auto-layout-101/
You need to use Auto layouts and size classes for this.
Auto layouts:
Auto Layout dynamically calculates the size and position of all the views in your view hierarchy, based on constraints placed on those views. For example, you can constrain a button so that it is horizontally centered with an Image view and so that the button’s top edge always remains 8 points below the image’s bottom. If the image view’s size or position changes, the button’s position automatically adjusts to match.
This constraint-based approach to design allows you to build user interfaces that dynamically respond to both internal and external changes.
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/
Size classes:
Use size classes to enable a storyboard or xib file to work with all available screen sizes. This enables the user interface of your app to work on any iOS device.
With size classes, a storyboard or xib file can be used for any available screen area. You build your interface as it will look in most sizes, then update only the parts that need to change when the available screen size changes.
https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/AboutAdaptiveSizeDesign.html

How to deal with adaptive layout in Xcode

I'm new to iOS development and I'm having some issues with the auto layout feature in Xcode 7.
As the image attached, my circle progression view doesn't scales fine for the 3.5" iPhone and it certainly looks fine with the other size classes.
Is there a minimum size limit for UIView in Xcode?
Also there are two text label in the circle view and no matter how I constraint them, their size just won't change across iPhone size classes.
As you might notice I have a SOS button which is a button embedded with an image I designed. I have added some constraints similar to the circle progression view but it doesn't work at all.
Is there any good guides/tutorial where it specifically teaches how to manage iOS element sizes across all iPhone size classes?
You can set the controls heights and the spacing bitween them to be related to the main view height. That will make them shrink little bit in 3.5 inches screen. You can then play around the constraints values to find the most convenient to you. like this

Xcode 6 Do I have to make an application for every screen size?

So I have created my webapplication and when I build it in xcode I have to under "Simulated Metrics" > "Size" set it to 3.5 inch since I have a iPhone 4s.
When I build it and run on the iPhone everything looks perfect, but I want this application to run on iPhone 5/6 aswell, but when I change the size to something else in the Simulated Metrics it gets really messed up on my iPhone. I'm using a webview of my responsive website which shouldn't really care about the screensize but I believe Xcode does. So, is there a solution where xcode automatically detects screensize and makes the webview take the whole viewcontroller? Or do I have to create a application for every screensize?
So, is there a solution where xcode automatically detects screensize
and makes the webview take the whole viewcontroller?
Yes - xcode provides ways to do this. Either through auto layout or using springs and struts. With auto layout you will define a set of rules that your UI will follow. If you want 1 layout for all devices it's fairly straightforward and any tutorial will get you started. with springs and struts you just tell your views how they should grow/stay put depending on screen growing. check this out for a quick introduction to auto layout: http://www.raywenderlich.com/83129/beginning-auto-layout-tutorial-swift-part-1
Or do I have to create a application for every screensize?
Nope.. but you can set different constraints in auto layout for your views to behave very different on different sizes. This gets a little more tricky..
No you do not make a new app for every potential screen size, instead you uses XCode's "auto layout" with a set of "constraints" controlling the size and placement of your widgets. A constraint can be a fixed size for the widget or a given distance relative to another widget (or the parent's border). It is not necessarily a fixed distance, it can also be "my widget needs to be less than 10 pixels apart from this other widget".
The "Simulated Metrics" you refer to in the bottom of the drawing area is only a visual help when laying out you your GUI, it has no effect at runtime. You can safely stick with "Any" width and height.
Read this tutorial to understand XCode's auto layout and constraints:
http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1
Quickly, these are the tools used to setup constraints:
In your case, assuming you have only one big view (your webview), you need to set up 4 constraints stating that the webview's left, right, top and bottom sides are 0 pixels away from the parent's left, right, top and bottom borders respectively:
Lay your webview in the middle of your drawing area
Click on it
Click the "pin" button; you will see this view:
Look at the upper part of the dialog: the small square in the middle represents the view you just clicked and the four red lines represent the distances to the parent's side. Set them all to 0 and click "Add 4 constraints". (Note: the lines are initially dashed when not selected; they turn into solid lines when activated.)
Now the frames in the drawing area do not represent what you were expecting: your webview is still in the middle where you placed it initially and does not fill all the available space; XCode is aware that there is a mismatch between the frames and the constraints and shows a warning. You need to update the frames: click the "issues" button and "update all frames".
The image comes from this question on SO:
What is "Constrain to margin" in Storyboard in Xcode 6
Read it to learn about the "Constraints to margin" switch.

Resources