UIbutton stretching after runtime - ios

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

Related

iOS: How to get the constraints for button inside horizontal stack-view right?

I am new to iOS development and am following the udacity course for iOS development. I am stuck at the lesson whose link I have given below.
https://classroom.udacity.com/courses/ud585/lessons/6991272849/concepts/72642287220923#
I am unable to reduce the size of stop button in the last stack view. I tried reducing the size of the button by setting the width constraint of the button to 64 and height of stack-view containing that button to 64. But XCode gives error of conflicting constraints.
I am attaching screenshots below for reference.
What I actually want is:
https://d17h27t6h515a5.cloudfront.net/topher/2016/November/582a8006_interface/interface.png
You can take a uiview inside your last stack and then You can take your stop button. Then you can change properties of your "stop" UIbutton.
Another option if you want to keep the existing constraints you have in the screenshot from the course is to change the properties of the stack in Interface builder.
For topmost stack
set alignment to fill, and distribution to fill proportionally.
For your stack at bottom with button in it -
set alignment to centre and distribution equal centering.
You may be getting the conflicting constraints because when you tried to "change" the height and width you actually added additional height and width constraints. Check them in the measurements editor and if your see constraints with the >= symbol, you should delete those. Click edit on the height and width remaining to make the changes. This should fix the problem. You may need to clean and build as well.
Hope that helps!

Swift - Why ScrollView not full screen?

I had inserted a ScrollView into UIViewController and dragged ScrollView to fill the space between the navigation bar and the RAM label below:
But when I run the app, the ScrollView does not fill the space:
Please help me! Thank you very much.
P/S: Sorry for my english is bad.
The scroll view is not covering up the whole thing because you are running the app on a much bigger phone. The simplest solution is to run the app on iPhone 5.
However, if you want to solve the problem on all sizes of iOS device, you need to add constraints.
Constraints are things that tells a view when and how much it should resize and where it should be positioned.
To add a constraint, just select the view you wish to add a constraint to and go to the bottom right corner. You will see 4 buttons:
The leftmost button is used to embedding views in stack views. This is a feature of iOS 9. If your deployment target is lower, just ignore it.
The second button to the left is for adding constraints related to alignment - where the edges of the views are, what its baseline is and where it is positioned in the X and Y axes:
The third button to the left is used to add constraint related to margins, width, height and how the width and height should change when it is asked to resize (keep the aspect ratio, for example):
The rightmost button is used to let Xcode decide what constraints you should add. And I think most of the times its choices are okay. Sometimes though, you still need to do some tweaking before it works.
"So... what constraints should I add?" you asked.
Well, I think I should teach you how to think when you want to add a constraint. This way, you can figure it out yourself in the future.
You should first let Xcode guess what constraints you want. Just click the rightmost button and click "Reset to Suggested Constraints". This can save a lot of work if Xcode can get it right. So remember to always do this first.
Then, run your app on various devices and see if the view's position, size, and alignment are as you expected. If it is not, you might have to add and/or remove some constraints.
For example, if you found that your view is always the same size on different devices, (that could be bad because it means that some content my go out of view on smaller devices) it's probably because Xcode added a width and/or height constraint to the view. You should delete that so that the view's width and/or height is not fixed.
You can find your view's constraints in the view hierarchy:
Just select the constraint and press delete.
Uncheck Adjust subview option and add
scrollview.view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
You need to add constraints for your scrollView. Set the leading and trailing constraints to 0. Pin the height of your scrollView and also don't forget to set the top layout constraint. You can either pin the height or add bottom layout constraint to your page control.
Constraints are very important and its even more important to set it correctly. Check the Apple Documentation - Working with constraints in IB
Uncheck constrain to margins and add 0 every one of the four limits of spacing to nearest neighbour.
My guess (from the little information we have) is that you are creating a constraint from your scrollview to the top of your view with a value equal to the height of the navigation bar. Set the value of this constraint to 0.
Just set the 4 constraints to 0 to the area you need and then uncheck the "Content Layout Guides" checkbox in the constraints tab here. It will automatically adjust to the area you have specified.
The checkbox to uncheck

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.

ios - UIScrollview w/ autolayout on Xcode 6

I have been struggling for days with this implementation, and even though I have tried to do every tutorial I found on the web, I still cannot make things work the way I want.
Basically, I am trying to put my login form in a scrollview, so that it takes the whole screen at first (and on all iPhones / iPads), and if the keyboard appears everything should move. The problem IS, my view doesn't take the whole screen... Either it is too large, or too high, even though in Interface Builder everything seams correct (from layout to constraints). Below and image of the layout I want to achieve (I am using an universal storyboard, with Size Classes and Autolayout enabled):
http://img4.hostingpics.net/pics/829115app.png
Can someone point me out on achieving this layout ?
Thanks in advance.
I would suggest pinning top, leading and trailing spaces of your scroll view to its superview. And set a bottom space constraint less or equal to the keyboard's height if you set it to 0, the scroll view won't be able to resize.
With your form layout set vertical center constraints and top space to superview constraints for your top label being more or equal than the distance you set in the IB, and then you can set relative space constraints between each of the components.
Hope I answered your question.
Edit: Just the provided project and got it working. I think the problem is caused by it being a containerView inside a scrollView. And both the container and the scrollViews content view adapt to the size of its subviews. Because of that, setting relative constraints won't help.
What I did was to set an explicit size (screen's size) to the containerView and setting setTranslatesAutoresizingMaskIntoConstraints(true) to it.
I modified your project and uploaded it here

How to Manage Complicated Views Hierarchy using Auto-layout in iOS Sdk

I am working on an iOS application and it will be compatible for all devices iPhone 3.5, 4, 4.7, 5.5 inches and iPad also.
I am designing a screen it has 5 subviews, as i view this screen in different screen it does not resize properly.
I am using Auto-layout to manage that screen and I have set the every possible constraint to manage this screen.I have to manage the Subviews height and width as the devices changes.
Following are the steps taken care by me
1)Set the Equal Height Constraint for all subviews
2)Equal Width Constraint for TopView1 and TopView2 set the High priority and set the proper Horizontal spacing, Leading, trailing edges for these two top spaces accordingly.
3)Equal Width Constraint for MiddleView1 , MiddleView2 and MiddleView3 and set the High priority and set the proper Horizontal spacing for these two and vertical spaces from TOpViews accordingly.
4)Set the Width Constraint for bottomView and set the High priority and set the proper Horizontal spacing for these two and vertical spaces from MiddleViews and bottom spaces accordingly.
Prior to this i have not used auto-layout in such complicated way.
Following are the screen shotes depict my problem.
EDIT:
#Ash Furrow please see attached a screen shotes with constraints.
I am laying out the base screen on Any Width Any Height
Please anybody suggest me how should i manage this view
Hmm. Looking at what you've done and the feedback to your question, everything seems correct.
I think I see the problem. The issue doesn't appear to be with your storyboard (here in case anyone is interested), but rather the use of Xcode. Instead of changing the view controller's simulated "Size" metric, use the Assistant Editor to view a preview of whatever device you want. I've tried that, and ran the code, and in both cases they appear to work.
So to recap, use the Preview in the Assistant Editor instead of changing the view controller's properties to resize a view hierarchy.

Resources