Stack View example from the book "Beginning iOS 9 Programming with Swift" - ios

So I was following the textbook instruction (beginning ios 9 programming with swift) from Chapter 6 to learn Stack View in Swift. However, I can't get the example to work. The image is just not displayed correctly in any version of simulator. So this is a vertical stack view with two constraints, "vertical spacing to top layout guide" and "center horizontally in container".
Any suggestions or similar problems that got solved?
Thanks!
Below are the wrong and right images:

First, get your wardRobe label with this setting, I set the Font size to 40, you can customize the way you want.
Second, get the other label like so :
Again, you can customize the settings of the label
Next, set the width of the two to be the same so that it looks like this:
Awesome, time for introducing player of the game, The Stack View:
Click on the button in your main storyboard (see below)
Next, click one of your labels and hold command, then select the other label too. (Both labels should be selected together). Next, click on this button in your storyboard:
Your stack view should look something like this :
Now, to resize the image, tick select the width and height and type in the desired values, and click add constraint:
Now, go to your view hierarchy and click add missing constraints, and you are done!
It should say, no layout issues like so
Update: Resolving auto lay out across different configurations:
Select your stackView like so:
Next, tick Horizontally in container and then add 1 constraint
Finally, go to view hierarchy and click fix misplacement. That should sort everything
Final preview

Related

MarqueeLabel text won't scroll

I want to display a label that continuously scrolls horizontally if it doesn't fit its superview. Kind of like on television when you have info showing up at the bottom with scrolling text.
I use MarqueeLabel/Swift pod
I created a sample project with a simple label that doesn't fit its superview:
I set its class as a MarqueeLabel:
I added some settings just as shown in their demo project:
And finally added this in code, same thing, I copied/pasted it from their demo (sorry for the image this time I couldn't get the formatting to work on stack-overflow!):
Any suggestion ? My only doubt is that when I set the class to be a MarqueeLabel, it automatically sets the Module as "MarqueeLabel" whereas I would have expected it to name it after my project name : "TestMarquee"
The problem maybe is the fact that your label view is out of the visual bounds of the parent view, thus you might need to add constraints to it.
I suggest you limit the label with 10-10 constraints on the left and right side respectively, and center it horizontally and vertically.
This way, the parent view controller will be able to layout it properly for you.

What must I do to add a constraint to a view in a Storyboard launch file?

I added a Storyboard launch file to upgrade a project from iOS 4 to iOS 9. The app runs in the Simulator but nothing I do will centre views for iPhone5 and iPhone 6 or scale them to fill the frame. Instead they look like bonsai versions of an iPhone 4 pinned in the top left of the screen.
When I try to add constraints to centre the views by following the procedure described here, the menu will not give me options to choose the view I need to constrain.
Instead I get this.
The bottom two - Horizontally in Container and Vertically in Container - are the only options I can check. But pulling down on the arrow to the right of the text box only gives some of the options needed to Add Constraints
What do I need to do to enable the view option ?
EDIT 1.
And furthermore, when I try to constrain width and height I get this
All six options are in grey and none of the text boxes change
EDIT 2.
And using the Size Inspector to look at the view I am trying to centre and fill, the view has no constraints as shown below.
It is really starting to look like there is no simple way to launch an old app where views have been created programmatically and have Storyboard Launch automatically layout views to suit different iPhone screen sizes.
The "Horizontally in Container" and "Vertically in Container" checkboxes affect individual views. The are "binary operations". If you select more than one view, these checkboxes cause all the views to be centered in their container.
In contrast, the other checkboxes in this popup align groups of views.
If, for example, you select 4 views and click "leading edges" then it creates a set of constraints that line up the leading edges of all the selected views. (Under the covers it creates enough pairs of constraints to line up all the views by their leading edges.)
If you only have 1 view selected then only the bottom 2 checkboxes ("Horizontally in Container" and "Vertically in Container") are meaningful and the others are dimmed. As soon as you select more than one view the others should be enabled.
I had the same problem because on Show the Size inspector > Layout, "Autoresizing Mask" was selected, by changing the value for "inferred (Constrains)" it will resolve this problem
illustration
Select the view you would like to constrain. Then tap the button to the right of the one you have selected and use that menu to define your autolayout constraints. If you want it to work for all devices make sure your storyboard is on the wAny hAny size class setting.

iOS multiple screen not working?

I just started iOS programming and I want to make a login screen. I want to align center horizontally the everything on all devices like the iPhone 5 and iPhone 6 and iPhone 6 Plus but it's not working. I just put the elements on the screen. Anything else i need to do?
There are a few ways you can do this...auto layout as #ozgur said is your friend.
what I would do is throw them into a Stack View
To do that tap each horizontal group so for example "username" label AND the text field to the right and while they are both selected tap the icon at the bottom right that is on the left of the 4 icons (it has a downward arrow)...now do the same for the password...now do the same for the login button...now select all 3 stackviews and tap stackview again...
NOW....on the left navigation select the topmost stackview and then again on the bottom this time tap the small icon to the right of the stackview button...to make it simple and in the center tap horizontally in container and vertically in container then add the 2 constraints....
you will also need a height and width so now once more with the whole thing selected tap the |o| looking icon to the right...now give it the height and width you like but before you tap add 2 contraints at the bottom of that menu is a drop down menu..tap it and select "update all frames in container"
that should do it
Use AutoLayOut Constraints to fix your issue.
Contraints will set the spacing with the view.You have to apply it on every single element of your view.
if you only want every thing in center no different spacing for different iPhone use stack View. it will provide the best solution for you.
stackView means collection of your objects. it act as group of your elements so only apply constraints to your stackview.
Rule of thumb is to use Stack views and then use auto layout. Just put the Username and the text field in a stack view (horizontal stack view), same for the other) and then apply necessary spacing and/or autolayout constraints.
You need to learn how to work with Autolayout (I read you just started) and it could be hard concept to grasp at first (it was for me at least) but with practice, you'll master it.
If you do not use the stack view, you need to learn auto layout and size class for adapting the screen.
auto layout began in iOS 6, sizeclass began in iOS 8.
Here is the official apple documentation, to study it. https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/

Buttons look different on storyboard to view

Why is my the elements of my storyboard (buttons) showing up differently in the simulator to the device? I've got auto-layout on, and I've disabled size-classes but surely this shouldn't matter?
How it looks in the storyboard: http://s11.postimg.org/h1xbn5f9v/Screen_Shot_2016_02_09_at_18_57_54.png
It looks fine on the iPhone 5 (it matches the storyboard), although on the iPhone 6 and other devices it's different.
How it looks on the iPhone 6:
http://s17.postimg.org/tfdaagt3y/Screen_Shot_2016_02_09_at_18_58_39.jpg
I'm using a view to hold together the buttons and centered it horizontally.
Any ideas? I'm baffled. It seems I'll have to programmatically place it if this is the case...
From the images you've shared thus far, I don't see any constraints other than the height/width. You need to select the buttons of interest and then hold Control and drag to the bottom of the container, click "Vertical Spacing to Bottom Layout Guide" and then on the right panel, set Vertical Space Constant to adjust (incase you find yourself having to temporarily place the button in the container to get the "Vertical Spacing to Bottom Layout Guide" option when Control + Dragging). Looks like you'll need a negative number for the Vertical Spacing constant to achieve the buttons being slightly out the bottom of the container. Then "Update Frames for Selected View" by pressing Control + Option + =
You can also try clicking the little triangle icon and clicking "Add Missing Constraints." This will add missing constraints needed to keep your icons where they are in the Storyboard, but sometimes I've found that using this automatic feature doesn't achieve my exact intent.

Very basic AutoLayout

In a View Controller I have two UILabels.
I'm trying to start to understand the AutoLayout feature, but I'm finding tutorials using storyboards, complex layouts, scrollViews and other components that are absolutely more complex of what I'm trying to achieve and understand.
This is my very basic setup:
What I simply want - to start to dive in the subject - is:
have the distance between the two labels preserved when testing it on 3.5' and 4' screens
have the bottom and top margin of both the labels to be proportionally preserved when testing it on 3.5' and 4' screens. EDIT: please consider that the desired position it's not the exact center
I have tried to add constraints via the "Pin" option, but with no success at all. Maybe I should put them inside another view to act as a container?
This is a very easy question, but right now this AutoLayout feature is really confusing me. And maybe this can be a good starting point to understand what right now looks to me to be designed by a crazy scientist :)
Put both UILabels in a containerView, and align containerView vertically center.
I would support #RoboticCat for the statement he made. But I would try to deliver what I learned from those videos.
For first requirement:
Ctrl+click on any of the label & drag n drop it on another label. Select "Vertical spacing" option from the popup. this will full fill your first requirement.
For second one:
Ctrl+click on top label & drag n drop it on view of viewController. select "Top space..." option from popup.
repeat previous step between bottom label & view. This time select "Bottom space.. " option from popup.
Now navigate to Size Inspector of each label. There you can find the constraints just created. Click them to perform Edit.
Decrease priority of top & bottom constraints to 500 from concerned labels. This will solve your issue.
Let me know if more info needed.. :)

Resources