Two-step auto layout - ios

I am currently building a UI component for an iOS application. Generally, the component is supposed to be 350pt in width, which I set up inside the component using auto layout:
self.widthAnchor.constraint(equalToConstant: 350.0).isActive = true
Now, if there is not enough space (due to other constraints outside of the component), I want the component to become smaller, but not fluidly - I want the component to alternate between two possible sizes only - 350pt normally and 300pt width if there is no room to fit 350pt. Is this achievable somehow?
Example project:
https://www.icloud.com/iclouddrive/055TkWS8aLqDhFDOPi-7qi8EA#LayoutExample.zip
I created a very simplistic example project to illustrate the problem. It contains a custom component (MyComponent), which is a stack view with four buttons. This component is embedded in the main view controller and auto layout is setup to ensure it doesn't flow out of the screen.
This works well on larger devices, but the component is too large for the screen of iPhone SE and iPhone 4. In these cases, I would like the component to become another size (in the project, the component is 500pt regularly and should be 300pt on smaller devices).
Reasons
The reason I am looking for this behaviour are pretty much all design related. This component is not supposed to exist in a variety of sizes, but only in two specific sizes - a "normal" size and a smaller size for cases where the normal size doesn't fit.

Related

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

iOS Simulator not displaying correctly in Xcode 6

I've been having such a hard time trying to figure out how this thing works. It's so random and I have no idea what else to try. I've looked up multiple articles on this issue and everyone just says change the scale. Changed the scale does not help, it's got nothing to do with what's happening here. I'm not sure if this is related to the bottom of Xcode where you can change the dimensions (Any vs Any / Any vs Regular Height, etc...) I've asked my mobile development teacher at school as well and he couldn't figure it out either. Any help would be greatly appreciated!!
Picture below:
http://tinypic.com/r/281fw5w/8
Your problem is not scaling. What you need to look at is auto layout and constraints.
You can use the icons in the lower right edge of the interface builder to get at them or control drag from a view controller (like a button, label, etc.) to the containing view (or any other view controller for that matter.) Usually, the main view window itself. When you release you can now add constraints to "attach" the element to that other element relatively. For instance, you could attach the things on the left to the left side and the things on the right to the right side. Now, regardless of the dimension of the actual device screen, those elements will appear in those locations relative to the device screen.
The problem is that the position of elements from your perspective is right for the canvas you see in the Interface builder, but once the app is run, the real canvas has different dimensions.
To manage the position, size and other attributes of UI elements, there is a system called AutoLayout.
It is quite ingenious because it is similar to natural language.
For example "I want this element to be in the middle of the screen."
or
"I want this element to be 20 pixels from the left corner and 57 pixels from the element that is above this element."
By combining these rules you basically create a set of layout constraints, that are applied in runtime to the view hierarchy and view are laid out properly.
Autolayout allows for very sophisticated layouts.
Another aspect you need to take into account that you might want your app to look well in all form factors from 3.5 inch iPhone up to iPad air.
Since these devices differ considerably in size, Apple introduced an abstraction called Size Class.
Size Class is an abstraction on top of concrete size. Concrete iOS devices have vey concrete dimensions. But in natural language you often say it's big ,or small ,or normal. And this the level of abstraction size classes use.
For each size class you can have a particular set of auto layout constraints.
So by combining AutoLayout and SizeClasses, Apple solved the problem oh how to have one application but one that can still accommodate specific form factors and can adjust its layout to them.
In Xcode6, all storyboards/xib files have autolayout & sizeclasses enabled by default. Interface builder provides you with a comfortable environment where you can set up your layout by creating constraints for each size class combination.

iOS stretching to fit all screen sizes?

So I've been making an app for iOS lately, and I've come across a dilemma. I'm making a mini synth, and I started making the GUI. I started making the keyboard (2 octaves for now) and measured the approximate length of the white keys. It all worked great, until I ran the thing on a 4 inch iPhone.. The keys were too small, they didn't cover the full screen etc... So I thought I could put the individual key images into a container which will stretch depending on the resolution of the iPhone, thus the images inside (the keys themselves) will stretch in percentage according to the size of the container.
Thing is I have no idea whatsoever of how I should go about it. What element should I use to contain the images/keys?
Generally what you want to do with new apps is make use of auto layout. This lets you set up the relationships between various UI elements in a way that is independent of screen size.
If you're creating views directly on the storyboard, you can add the constraints there.
On the other hand, if you create the views in code, you'll need to use the NSLayoutConstraint class to create them (more information is available in the documentation, especially for the Visual Format Language).
In this example, it sounds like what you're after is a constraint on the container which pins it to its superview, eg:
#H:|[container]|
#V:|[container]|
along with proportional constraints on the keys (setting their width to, for example, a given fraction of the parent container's width).
Note that this approach (using auto layout, but with the same base layout) may not be appropriate in all circumstances.
In particular, it may actually make sense to display a completely different layout on a tablet, where you have a lot more space for the keyboard.

Margin and padding for UI elements in iOS

I have so far developed for Android. There, you can set layoutMargin and padding for almost every UI element (when you describe UI in xml file).
How is this done in code for UIViews in iOS (if there is a common command)?
Tnx
iOS employs a different approach to control placement - there's no layouting engine, you specify absolute coordinates and size for all controls. Kinda like the AbsoluteLayout in Android.
So the concepts of margin and padding don't really apply - the gaps between adjacent controls are completely up to you, they're not computed by the system. Same for sizes.
This makes it more difficult to implement the scenario of "make this control as large as it needs to be for its text". However, you won't run into rogue line wraps.
EDIT: AbsoluteLayout is deprecated these days.
You lay out your UI elements either in code or using Interface Builder (or the .xib editor in Xcode 4). You'll specify actual coordinates for your views, but you can also specify the resizing behavior for each view. So, you can say that a particular view should always keep the same size and remain horizontally centered, or that it should keep it's size and maintain the margin on the left, or that it should stretch to keep both left and right margins, etc. Look for the "autosizing" section in IB, or set the autoresizingMask property of any view in code.
In ios you can use Interface Builder for design. You can still use xml, but no one use this.

Resources