I use two buttons in a camera app to track the zoom of said camera. My goal is to reduce the height & width of 1x if 2x is selected and vice-versa but it doesn't seem to work.
I tried button.frame.size.height = 8 button.frame.size.width = 8 and the height & width always remain the same. I even tried 500 to see if it would make a difference and it didn't.
I also tried button.size(CGFloat) to no avail...
I should probably mention that those buttons are nested in a StackView and the app is built on Storyboards. Also, the app is designed to be used on iOS 13+.
Thanks in advance!
UIStackView is using Auto Layout for arranging its subviews. Therefore, direct modifications of the button's frame will have no effect because Auto Layout will anyway recalculate all frames according to the active constraints on the next layout pass.
Since you're using a storyboard, here's how to solve it:
Open the storyboard, select your button and create a width and a height constraint for it.
Create IBOutlets for each of these constraints in your view controller. For the sake of the example let's say you name it buttonWidthConstraint and buttonHeightConstraint.
Whenever you want to change the size of the button, do this:
buttonWidthConstraint.constant = 8
buttonHeightConstraint.constant = 8
Related
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.
I'm struggling a bit with constraints on iOS with the differents screens sizes nowadays.
I tried google and stackoverflow to find a solution but my english does not seem fluent enough to find an answer.
I got 4 buttons verticaly aligned, the first and the last one are constrained by the edges of screens (kinda easy). But I'm really struggling with the constrains of the 2 middle buttons. I can't find a way to make them equally spaced from the left and right buttons on every size screens.
Is there something easy and tricky to make these constraints right ? Or am I doing it wrong and should I try to do it programmaticaly ?
Thanks
Add three invisible views in middle of each button, make their width equal with each other with constraint then your four buttons will be equally spaced. For the Y then you just need to pin it at your desired place.
If you are going to support only iOS 9 and higher, then use a UIStackView.
The solutions to this problem is very simple. This can be solved using the concept of "spacer views". You have to place invisible views between each red coloured view. You would be requiring 3 in your case. Then make their background as clear colour. .
Next, make their width equal and constrain their edges to the views that are after and before that view. You then define the size for red coloured view.
REMEMBER dont give the "clear coloured views" any fixed width. It would be determined by the runtime.This would solve the problem. Tell me if any more information is required.
Here is a blog post for the solution for this
http://adamdelong.com/fluid-layouts-with-auto-layout-size-classes-spacer-views-and-constraint-priorities/
This is the youtube video for this
https://www.youtube.com/watch?v=eSG-3-QpmWk&feature=youtu.be
Besides Tj3n's answer with views between buttons, you could use
A UIStackView (iOS 9!) where you use for settings Axis: horizontal, Distribution: equal spacing
A Toolbar (depends on what you want to do with your buttons) with toolbar items and flexible Space between them
Why not size classes? Apple introduced the concept of adaptive user interfaces in iOS 8 relying on a combination of Auto Layout and size classes.
If you aren't aware of what is size classes, there are plenty of tutorials available, please find one.
Summary: Apple very cleverly removed two story borads for iphone and ipad and made a single story board for universal app. No you dont have to struggle trying to apply autolayout constraint that satisfies all the screen sizes :)
Below are few of the size classes and their meaning :)
Regular width x Regular Height ----> iPad Potrait mode/ipad landscape mode
Compact width x Regular Height ----> iPhone 6 plus,iPhone 6,iPhone 5s,iPhone 5,iPhone 4s potrait mode
Compact width X Compact Height ----> iPhone 6,iPhone 5s,iPhone 5,iPhone 4s landscape mode
Regular width x Compact Height ----> iphone 6 plus landscape mode.
You can select the size classes you want to support from story board :) and start applying constraints specific to each size classes (like buttons in middle) or if you have generic one (like your buttons fixed to screen) for all the size classes.
You can deploy, remove, reuse or delete the constraints form various size classes.
SUMMARY: Buddy, If you are not using size class yet, its a high time to start using it :) There is a wonderful video on it in apple WWDC sessions 2014 i believe. Download, watch, start playing with it.
Happy coding :)
you can use equations to get this appearance.
use views' trailing points to get this.
View1.trailing = superview.trailing*(2.0f/9.0f)
View2.trailing = superview.trailing*(4.0f/9.0f)
View3.trailing = superview.trailing*(6.0f/9.0f)
View4.trailing = superview.trailing*(8.0f/9.0f)
if you make View1.width = superview.width*(1/9.0f) you can achieve what you want.
there are a lot of similar solutions for this issue. but the base is using trailing points.
It may also be done with using centerX positions of Views.
View1.centerX = superview.trailing*(3.0f/15.0f)
View2.centerX = superview.trailing*(6.0f/15.0f)
View3.centerX = superview.trailing*(9.0f/15.0f)
View4.centerX = superview.trailing*(12.0f/15.0f)
Thanks to LearneriOS answer, I solved my problem.
In order to get my wished result, I created 3 views with 10 width.
My first and my last button were already constrained. I constrained my first extra view to my first button with Horizontal spacing then i constrained my first extra view with the Center vertically in container. I then constrained my extra view to his own width and heights.
There come the important part: I did go on the constraint menus and selected the width constraint. The value inside was still 10 but I did change the priority from 1000 to 750.
Then I did copy my first extra view and constrained all of them to the nearest buttons, the same way I did with my first entra view but I removed their width constraint (to all the extra views but the first one) and constrained the extra views with the first one by plugging the: Equals Width.
Then I got my result, I hope it was clear enough and thanks again.
I am working an ios app using Xcode 7.0.1 and swift 2.0. I want to design app for all ios devices iphone's and ipad's. I am using wAny, hAny in storyboard. I use constraints also. One of my screen i have some buttons and i want to change height of buttons. I add my storyboard screenshot .
Now my problem here is i want all the buttons same width and height at initial stage after that i need to change height. So i use equal height constraints. Initially it is working. How can i change height of one Button using swift code.
Note: I am using view to place the buttons because it have label inside so.
You can connect constraint of the button (which will provide height to all other buttons) as IBOutlet.
And if you connected try this
baseButtonHeightConstraint.constant = 70.0 //for example
Click on the button that you want to change the height, In the size inspector which is on the right top, change the height and save it. Try running then.
For my 1) portrait only 2) deployment target iOS7/iOS8 app, I have in my design UIButtons which have variable heights, for iPhone 5, iPhone 6 and iPhone 6+. I am trying to use auto layout for this. (Not using size classes though).
Using auto layout how can I specify variable height for UIButton for these 3 screen sizes. The buttons look fine on iPhones 5* models, but thinner on iphone 6/6+. Now in auto layout I can say height >= or = or <= say 55), but how do I specify 44 for iphone5, 55 for iphone6, 66 for iphone6+?
Is this something that I can achieve using only auto layout or do I need to manupulate (frames) it in code? What is the point of me using auto layout then?
In addition to frames my designs also specify different font sizes. How do I go about this. Any best known methods for this, Stack-O friends .. ??
You are correct to ask "what is the point of auto-layout if I have to manipulate frames directly"? Thankfully, you don't have to.
An easy way of doing it is specifying the height in relation to a view of standard height (think a view that fits the whole screen).
For example, we can set our button's height to equal half the height of the view.
This way, the button is always going to scale with the view, either upwards or downwards (size-wise). Auto-layout will guarantee that the relation between them will always be 1/2.
Now the button will be half the size of its superview, regardless of size.
It sounds like you need to be modifying the height constraints constant value.
In your xib/storyboard, create an outlet to your view/controller for the height constraint.
At runtime, probably in viewDidLoad, you will work out which device you're on, and then just change the constant of the height constraint.
CGFloat height;
// if iPhone 5
// height = 44
// else..........
self.buttonHeightConstraint.constant = height;
This will automatically trigger a flag that tells AutoLayout to recalculate frames and layout the view again.
I managed this by adding a Height-Constraint to the UIButton. Make an Outlet and you can set the Height in your UIViewController subclass with _myConstraint.constant = 55;
Hey my iOS app is very simple and instead of messing with the auto-layout feature of Xcode is there a way to just programmatically size the screen for each resolution. I am using the storyboard feature of Xcode, but I can't seem to figure this out. I googled it quite extensively, the only thing I found was Editor > Resolve Auto Layout Issues > Reset to...
That works well, except it always squishes my images. I try to set a forced height on them and then everything goes crazy!
So Is there any techniques that shrink everything in the view by 10% vertically?
Thanks, Krkto
-BTW screen is a UIView
-edit- also When I set the autolayout to "Reset to suggested contraints in..." and I call a animation that scrolls the view up, it scrolls the assets up slowly, while the background goes up in a normal fashion.
Here is how I would do it with AutoLayout.
Setup your view in IB / Storyboard
Setup the constraints
Since you want to scale subviews - set their height and width constraints.
So - Width constraint:
Do this by
ctrl + drag inside the UIView from left to right (Or visa versa)
When you stop dragging a pop up will show with two options: choose Width.
So the same for height - just ctrl + drag from within the UIView you want to scale, top to bottom or bottom to top and when the pop up shows, click height.
Now that your constraints are set - find them in the left panel that shows all your UIView objects for your IB / Storyboard. They will be in the same UIView you made them in.
Now, ctrl + drag them to your view controller - these will create NSLayoutConstraint `IBOutLets
Example:
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
Now - when you want to update the height constraint (and width) - in code, do something like this:
self. heightConstraint.constant = 10; // new Size
[self.view layoutSubviews]; //Causes setNeedsDisplay to be called on all subviews in UIView
That's all there is to it. You could put this code inside a UIView animation and it will animate nicely, too. Just use some simple math to calculate the size to whatever % you want.
If you look in IB / Storyboard - you will see where the constraints in the left side of Xcode are shown, it will give you there current constant value.
Example:
Hope this helps.
What your missing is constraints. raywenderlich on autolayout
This tutorial provides what I believe your looking for.
It looks like what #krtko really wanted was to turn off Auto-Layout, Size Classes and put the app into auto-scale mode. For simpler screens this is just an easier way to handle the four screen sizes than Auto Layout. The "struts and springs" (Autoresizing) function can still be used to define what happens as the screen real-estate grows or shrinks (particularly in the case of iPhone 4 / iPhone-on-iPad mode).
The key is to use the UILaunchImages key in info.plist and specify images for only 320x480 and 320x568. This throws app into a mode where the 6 and 6+ sizes are automatically displayed as an iPhone 5 scaled up. You lose the benefits of high resolution, but gain a lot of time not having to play with AutoLayout and deliver lots of image files in all the resolutions. Great for MVPs and quick prototypes.
https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW28
Note that in this environment, the Assistant Editor / Preview function does not work properly; it shows what the view would look like if rendered through the usual scaling mechanisms, not the automatic scaling that is used when compiled. The simulator will show the correct layout.