iOS - Top and bottom margin on control in horizontal UIStackView - ios

I am designing a prototype cell in Xcode to display events in my iOS app. At the moment I have the following design:
The cell is constructed like this:
Stack View
Label (99:99)
View (Orange bar)
Label (Event title)
I would like to have a top and bottom margin on the orange bar of 2 pixels (basically not have the orange bar occupy the full height of the cell). I tried to add a top and bottom constraint on the View which looks like it is working, however Xcode is complaining about this solution (see screenshot).
Can anybody help me on how I could best accomplish this?

It is because of you have just set top and bottom constrain. you also need to add other constrain. Just click on red round. and set add missing constraint.
set constrain as shown in below.
Image of the constrain
Edit:-
View Hierarchy:-

Set View (Orange bar) height less than 4 pixels.After that set top space and bottom space of orange bar view to superview 2 pixels.

Related

Autolayout with specific constraints

I have problem or maybe wrong understanding of constraints in general with view controller form photo below.
I already implemented scroll view and it is working properly. Photo below showing Content View with two subviews (with the same name: View). What I would like to achieve is to set first View (the blue one) to be exactly on edges of the screen and second one (green view) to be visible only when user scroll down on this page. Is it possible to achieve these just using autolayuots or I need to do some hard code work. What is the best approaches for solving this taking in account that I need this app to work with all king of iPhones.
At the bottom of the green view is located tab bar but it is not visible on this photo.
This is pretty straight-forward...
Pin your scroll view to Zero on all four sides.
set Blue Top, Leading and Trailing all to Zero to superview (the scroll view)
set Blue width equal to scroll view width
set Blue height equal to scroll view height
set Green width and height each equal to Blue width and height
set Green "center horizontally" to Blue
set Green Top to Zero from Blue bottom (vertical spacing)
set Green Bottom to Zero to superview (the scroll view)
That should do it :)
Edit: whoops, corrected step 4 (Blue height should be equal to scroll view height, not multiplied by 0.5)

Xcode 7 Vertical Scroll View Issues

I'm having trouble creating a vertical only scrollview in Xcode 7.3. I've followed a couple of tutorials on SO and other places but for some reason it seems that my topmost view's width extends past the screen or is shifted to the left. The scroll view is restricted to vertical scrolling only, which is my intent but the content appears partially off the screen.
I try to horizontally center buttons and pin to the top and bottom of the content view. On the simulator they appear half off the screen to the left but they appear fine in the storyboard with no constraint issues. I even preview the view 3.5, 4, and 4.7 inch screens in the assistant editor and it looks as I expect it. But when I run it on the simulator it's messed up. Also the background of the Content View is supposed to be white (for testing purposes) but it's showing the gray color of the top-most view.
This is what I do along with pictures of my constraints
Add Scroll View and pin to the super views top, bottom, left, right (0,0,0,0)
Add Content View and pin to Scroll View top, bottom, left, right (0,0,0,0) and also add a constant height of 1000.
I ctrl+drag from Content View to View (parent of Scroll View) and set equal width constraint.
Place 2 buttons inside Content View pin 1 to the top and the other to the bottom. Both are horizontally centered as well.
This is what it looks like on the simulator.
Simulator-Actual
And this is what I its supposed to look like on the iPhone screens.
Storyboard-Expected
Give it horizontal centre constraint and than check
I think you have set all constrain correctly . But still check this once again.
Step 1 :- Remove tick Adjust scrollview insets
Step 2 :- As you said Give Leading , Trialing , Top , Bottom = 0 .
Step 3 :- same as step , set Leading , Trialing , Top , Bottom = 0 from superview
Warning :- You need to check both the constrain inspector because sometimes it takes Leading space from -20.
Step 4 :- Set constrain to 1 st button Top , Leading , Trialing
Step 5 :- Set Leading , Trialing , Vertical space to button 1 and Bottom (bottom from UIView)
If you give vertical spacing to button1 and give bottom to view then you dont need to set contentsize or else , it will scroll Automatically.
I hope this will be helpful for you.

iOS 8 after hiding tab bar - reuse this space to display a text box

I can hide the tab bar programmatically. The view underneath is a scrollview displaying content. When I scroll up/down I can see the white space at the bottom where the tab bar used to be. My content is not able to use that space.
I want to display a text box in that white space along with some buttons. Any suggestions on how can I do that?
Solution:
Thanks to "SwiftArchitect" advice. I was able to fix this by adding bottom constraints for the ScrollView. Now the scrollview goes till the bottom of the screen.
As often is the case with AutoLayout, this question becomes trivial when using Storyboard and either the ViewLayoutAssistant or an image which will show evidence of resizing.
Let's assume that you are adding 4 constraints for your scroll view:
vertical space view.top (0) equal to Top Layout Guide.bottom
vertical space view.bottom (0) equal to Bottom Layout Guide.top
and 4. horizontal leading and trailing space
...then when hiding the tabBar:
if let tabBarController = self.tabBarController {
tabBarController.tabBar.hidden = true
}
the view.bottom, anchored to the Bottom Layout Guide.top, will now stretch all the way to the bottom. If you add a UILabel above that line, it will show.
Here is what it would look like in the Storyboard:
When executed with the ViewLayoutAssistant, the UIViewController that hides the navBar as follow will look like this (notice that the view now stretches all the way to the bottom since the Bottom.Layout.Guide moved down):

Resize proportionnaly between iphone 4 and 5 screens with XCode

I am trying to use auto layout in x code to correctly resize my view to switch between iphone 4 and 5.
I have a similar problem as the one presented in the picture. Let's consider the UILabel at the bottom is fixed.
I want the top UILabel to be at the middle of the space between the navigation bar and the bottom label. I don't know how to add a constraint that says "top space and bottom space must be equal". As a result, I can only fix one of the two constraints and I cannot get the expected result.
Thanks
The way I do this in IB, is to embed the top label in a UIView. Give that view a standard vertical spacing constraint to the top layout guide, and another to the bottom label. Also give it a fixed width. Give the label centerX and centerY constraints to this view, and it should keep it centered in different screen sizes.
How about adding a container view that fills up the space between the 1st label and the navigation bar, and then center the 2nd label inside the container view.

How to rearrange a constraint in iOS?

In XCode I'm working on a small view I use on several screens:
There is the black bar on the top. By default it has 2 constraints that defines a static distance between the bottom of the bar and the bottom of the view. So if I load this view on a large screen the top bar will be stretched out. I would like keep it fixed height and want to stretch the view in the center instead.
I've tried to delete the 2 constraints but they were re-created immediately. Also tried to set their value to 'auto' but that did not help either.
How can keep the top bar fixed and stick to the top - and having the view in the center dynamic height?
You'll want to add two new constraints. One that sets the top space of the top bar to superview and one that sets the height of the top bar. Once you add these constraints you should be able to remove the other two that are causing this issue. You can add these constraints using the middle button (pin) in the bottom right hand corner of the storyboard window:

Resources