XCode Attributes
Why are the Xcode 4 attributes for setting the frame disabled?
I see many posts on this and I did set the view controller simulated metrics to various settings. However, you can see in the image that the X value is 320 and the Y value is 250!
This would place the view controller way off center. This view controller is in a storyboard so does that make a difference? If I see to "free form" for simulated metrics I can change the width and height but the X and Y values are stubborn.
Why are the X and Y values weird and NOT changeable?
I was about to delete this question but answering it so that other poor souls can know the reason for this.
I completely missed the "Origin" setting in the attributes area. It's the box to the left of the coorindate values. For some reason it changed and I didn't notice.
I set it back to the upper-left so that the coordinates can be interpreted correctly.
Related
Why does my view debugger show a different value and how can I make my app match it?
My view debugger is showing this (this is the correct view):
But my iPhone is incorrectly showing this:
I spoke to someone I know and they said that the debugger lays out the subviews again. How can I trigger this same thing in my application?
This is what my hierarchy looks like:
---UICollectionView
----UICollectionViewCell
-----UIStackView (vertical, scale to fill)
------UILabel (hugging and compression are 1000 for vertical and horizontal)
-------UIImageView (aspect fill, hugging and compression are 750 for vertical and horizontal)
If I begin my app in portrait, all my views layout correctly. If I begin my app in landscape, then I have that incorrect overlay that you see in the image. Any help?
Thanks!
You should set imageView.clipToBounds = true (or any other UIView's) either in code or in IB.
Apple Documentation Reference
A Boolean value that determines whether subviews are confined to the
bounds of the view. Setting this value to true causes subviews to be clipped to the bounds of the receiver. If set to false, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is false.
I'm trying to make a divider, so what I do is:
1) Make a new View
2) Change the background to the color I want the divider to be
3) Control click and center it horizontally on a label on top of it
4) Compile, my view is gone.
5) Remove constraints, compile, my view is there.
6) Add left and right constraint for autosize, compile, view is gone.
It also states that the height of my view is ambiguous.
How can the view be gone by merely constraining it?
Screenshot:
Your view does not disappear. It just getting zero size.
When you remove constraints it won't iOS won't layout your view that is why it's size not changing.
And in you last case with leading/trailing constrains your view will calculate its width depending on it's superview width, but since system can't calculate height it gonna change it to zero.
Other views and like labels and images have content and with a help of defined intrinsic content size they system won't resize them to zero sizes but to their content size.
here is my output I tried in swift using Xcode and tried setting constraints as I do
I am trying to add constraints for the following to enable the buttons and label to adapt their size and font size to different screen sizes however everything I have tried so far has not worked, even when using Stack Views.
Could anyone help with this?
Storyboard Screenshot
My "breakthrough" in understanding and enjoying constraints was when I learned how to use the Constant and Multiplier attributes.
The four buttons appear to be about 80% of the width of the device. So, you could make a constraint between a button and the view and select "Equal Widths". Then, select the constraint and look at the Attributes Inspector. Change the Multiplier to 0.8 or whatever value you like. You will still need to set other constraints, such as center horizontally in container.
You could something similar with the horizontal buttons.
As for the text in the buttons, I needed this, too, and finally found the following (Objective-C):
button.titleLabel.adjustsFontSizeToFitWidth = YES;
Good morning,
I am new to Xcode and am learning to create iOS applications.
When I open a single view application and click on main.storyboard, my size is w Any h Any. When I decide to add a label and run the iOS simulator (iPhone 6 or iPhone 5S), the label appears somewhere else.
This is really frustrating and I have tried many approaches such as disabling use size classes, changing the storyboard size by clicking the w Any h Any button, and even messing with the constraints as mentioned here: Xcode 6 Storyboard the wrong size?
I am really trying to continue with this but I have seem to hit a wall for a couple of hours now, if someone could shed some light to why I am messing this up, that would be amazing.
EDIT: How can I get it to be a "normal" sized iPhone, such as the iPhone 5s?
You can click on the w Any h Any to change it to a normal iphone size by mousing over the squares and reading which devices they encompass.
You are going to have to use constraints though in order to make anything go where you want it to, I really didn't want to learn them but I couldn't do without them now: they are very useful.
EDIT
Constraints are simple in concept but can be tricky in certain situations:
For any view to have valid constraints that work correctly, it needs to know what the size of the view is and its position in it's "parent container" which is just whatever view or viewController it is inside of.
The little |-O-| shaped button and its neighboring buttons next to "w Any h Any" give you options for positioning and sizing the view. So if you click on a view and then click on that square button in the middle, check the width, height boxes and click the left and top lines in that top positioning thing with sizes in it like so:
Then click on add 4 constraints. You will notice blue lines appear around your view saying that it can properly put it where it needs to go when running the app. If there is any orange or red that means there are conflicting constraints on the view.
Sometimes that can mean you put to many constraints (more than you need) and you just need to delete them in size inspector tab. But more often than not, if that doesn't fix it, I've noticed that I usually have a neighboring view that isn't properly "constrained" and is actually the cause for the other views problems.
How can I get it to be a "normal" sized iPhone, such as the iPhone 5s
You don't. The view controller's main view will be resized correctly when the app runs (on a device or in the simulator), as appropriate for the device type and other aspects of its surroundings.
Your job is to use auto layout so that no matter how the view is resized, its subviews (labels and buttons and so forth) will look good. That is what auto layout is for - it's to help you compensate for the fact that you have no idea what the real size of this view will be at runtime.
I have read in numerous places that constraints in Cocoa AutoLayout act on the visual bounds of the control/view in question so the below is perplexing (2me):
In Xcode IB, I add/drag two (push button) buttons to a view (one underneath the other) and constrain them such that they have zero distance between themselves vertically. I expect two buttons that are flush against one another in the vertical direction.
The result is two buttons that DO have a small difference/space between them which can be verified visually or by looking at the alignment rectangle values in Xcode. In this instance the additional distance between them appears to be 1 point.
Below is a screenshot of the constraints amongst the simple view hierarchy:
Further, confirming the above, when you change the constant in the constraint to -1, the two buttons appear flush with one another.
The above is on OS X 10.10 (Yosemite).
Stranger still, in my actual application (the above is just a test case for the SO question) when I stack many NSButton vertically (programmatically and not via IB) I need to provid a constraint of -4.0 between them (as opposed to the -1.0 above) to make them flush. The bezelStyle for this button is as follows (Swift):
self.bezelStyle = NSBezelStyle.RoundedBezelStyle
[Although it doesn't make sense, I would be much more comfortable if it were uniformly -1.0 needed to make them flush!]
UPDATE I:
I think I have found why my application is behaving differently to the test example. I have used a NSBezelStyle.RoundedBezelStyle in code and set its height to be 24.0; various things point to this button style/type needing to have a fixed height (see IB where the height parameter is greyed out and fixed at 21.0 and cannot be changed!) The difference between 24 and 21 is 3.0 which is exactly what we are seeing is the difference in main application constraints -4.0 relative to rest example required -1.0
The moral of the story: don't use RoundedBezelStyle outside certain usage scenarios where variable height is needed! PostScript: RoundedRectBezelStyle seems to suffer the same malady!
That still leaves the question of why certain buttons in the test example are not flush with a constraint of -1.0 being required...
UPDATE II:
Further testing shows that I get different results depending on which "type" of button I choose. In code this would be linked to the bezelStyle property of the button.
The gradient button sits flush with a constraint of zero between buttons
The textured button seems to need to need a +1.0 constraint between buttons to avoid overlap
Most of the other buttons (especially those with rounded bezel styles) seem to need constraint of -1.0 between themselves to be flush
REVISED QUESTION (based on my findings):
Are the above intentional from Apple (is there some logical/rational explanation) OR are they bugs?
If the above is true, is it bad practise to code my user-interface (a spreadsheet-like grid) with these fudges for "flushness" built-in to how I layout my view?
How a view lays out in regards of it's bounds is up to the view itself.
What you have seen is the style of the button not covering then entire frame set up by autolayout. That is correct behaviour.