Constraint issues ios- Swift - ios

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.

Related

Swift: Programmatically change size of a button in runtime

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

Swift - UIButton is not pinning to the bottom of the screen

I am having trouble pinning the UIButton to the bottom of my UIViewController, (There is a tab bar if that helps).
Here are the constraints of the UIButton.
Originally, it all worked fine, but ever since I added iPad support to the application I have issues. on the iPad the UIButton is pinned to the bottom of the screen, directly above the tab bar. However on iPhone, there is this empty gap at the bottom.
What I Tried
Doing some research I saw that sometimes split view controller causes a grey bar, I added this line of code to resolve it and it fixed the bottom bar , but this constraint issues is still causing me a headache.
self.extendedLayoutIncludesOpaqueBars = true
iPad View Hierarchy
iPhone View Hierarchy
You have an option on Xcode to vary your constraint depending of the size of the screen.
You can create two constraints, one that works on iPad, one that works on "classic" iPhone.
Here, if I create a new constraint during the variation, I can then select on the right window which variation you want the constraint to be installed on.
There is a whole topic about that here
Remove current Constrain and Try unchecking "Constrain to margins" button.
Try setting a constraint to a View instead of a SafeArea
1- Give it a constant height for example 40
2- While setting constraints, uncheck (Constrain to margins)
3- Give it a proportional width constraint with the super view 0.8
Note1: If you don't want to give it a fixed height you can do the same step 3 but with height and not 0.8 but maybe 0.2 to less depending on your design.
Note2: You can use a scroll view, it will make it so easy for you

UIbutton stretching after runtime

I have form page with few textfields and a button.
I have added constraints for each component as well.
The issue is that the button getting stretched after running the app.
Please see below screenshots for my issue.
1- button size in storyboard
2- constraints
3- result in simulator
This is happening because the height of the device you running is larger than the device you have selected on your storyboard. To overcome this issue.
You can give a fixed height for the UIButton and remove the bottom layout constraint(lazy fix).
The better and easiest way is to use vertical UIStackView and give proportional heights to all the components. Read this article to gain more knowledge about UIStack views.
The height of the device you implement your constraints in is smaller than the device you test in , since you have leading , trailing , top and bottom your button will stretch depending on current device , so either remove the top/bottom constraint according to your design need
You can optionally add height to the button or leave it with it's intrinsic size

How to make same width for 5 button horizontally in xcode

I've designed those 5 button in my storyboard based on iPhone6. Now problem is my client told me to make their app can be workable in iPhone5 onwards. Current those 5 buttons in iPhone5 cannot be displayed proportionally as same width. That's why my question is please let me know how to make same width for 5 button horizontally in xcode?
Try this:
Add 5 UIButtons to yourViewController.
Highlight buttons
Click "Stack" icon at the bottom of the storyboard window.
On the UIStackView's attribute inspector, make sure it's "horizontal", distribution is set to "Fill Equally", and adjust the spacing to whatever you want.
Pin the UIStackView to left/right, top/bottom.
It should look something like this when you're done:
Using a stackView works great but for those whose target is below iOS 9, you can do it very simply with autolayout. Just select all 5 labels and use the "pin" icon on the bottom of the storyboard to check "equal width".
As already said in the comments you can create an Horizontal Stack View. Then add the 5 buttons to the Stack View.
Finally you can select the spacing by selecting the Stack View and, in the Attribute Inspector, increasing the Spacing field.

UIView Doesn't allow swipe down to see rest of the screen

I build a view that has contents which exceed the height of the iphone 6 screen vertically.
To test that out, I've set a specific height for my image (600pts) to guarantee that the whole content wouldn't fit on the screen of an iphone 6 and that you should swipe down to see the rest (the label "BYE").
However, when I run the simulator, I cannot swipe down to see the rest of the screen.
I've used Autolayout constraint and set a margin top/bottom between each components and the superview (I don't have any autolayout constraints issues/warning).
Here's the screenshot of the storyboard of my UIViewController:
Here's when I run the simulator, I cannot see the Label "BYE"(which is normal) but I cannot swipe down to see it.
I am using Xcode 6.1 and Swift.
Any suggestion and explanations?
Thanks a lot!
I have redone your screen and here are the steps. The reason for your issue is not setting proper constraints in autolayout. First you need to choose AnyWidth AnyHeight in Xcode which is in bottom of the storyboard.
Here is the screenshots of View in StoryBoard and its constraints
Constraints
Final Result in Simulator
Setting the constraint properly will work for you. Let me know if you have any issues in doing so. Sorry for the image i used its low quality one and its just for test.
EDIT
In Storyboard if you see the bottom of the screen you will see below image. In center of that image there is wAnyhAny.
you need add constraint to thoes components!
Otherwise, some components will be placed out of the screen!
You can learn how to use Autolayout from here
![enter image description here][1]At right bottom of storyboard in xcode you will see triangel Resolve autolayout isuue select in Reset to suggested constraint.

Resources