How to deal with different "misplaced view" warnings on every machine? - ios

What is causing them? How to get rid of them?
Create a storyboard, add some views. There are no warnings, so store/commit it.
When you open it on another dev machine, there is a chance, it will complain about "misplaced views". You can do the suggested quickfix to resolve it, but if you go back to the first machine, it will complain in the opposite direction.
I think it has to deal with the status bars, in this screenshot you see y=64 instead of y=44, but why!? It's the same project on both machines.
Anything I can set to make it behave the same everywhere?

1: Your other developer may have a different xCode with different set of warnings.
2: xCode constraint warnings may look mysterious but actually it's doing it's best to keep you alerted. When autolayout can not decide where a view should be placed, like left or right side of the parent view THEN it will try to warn you once by putting it on the left side then once by putting the view on the right side. Same goes for warnings..
DO THIS: before you end working, go through all the Size Classes and make sure you have 0 warnings left in any of the Size class.
BTW: that top constraint is sometimes sticking to a status bar height (20px) and sometimes not. Stick it to something else. Maybe setting the statusBar settings as "Inferred" would help.
How to work your way through the size classes
You start out with Any x Any size class -> setup all the constraints -> now start switching size classes -> if everything fits (no warning) even on the smallest device in Landscape mode THEN you are ok.
If something not fits or you want a bottom view bigger UIView on iPads for example THEN you switch from Any x Any to Regular x Regular (or where the warning is) -> and alter the same constraint just for that specific size class. "Install constraint" the UI says. U can add custom UI elements too btw.
See image, I have a bigger height for the bottom view in Regular x Regular (iPad landscape/portaite):
The result will be that now and in the future you are almost 100% adoptable to any device size that will appear on the markets = less maintenance time :)
vote up :)

Related

Xcode : changing view size depending on screen size

I would like to change view size depending on the screen size of the device.
For example, the view should not have the same size on iphone 4s and iphone 6.
The best way would be to use size class, but iphone 4s and iphone 6 are in the same.
Is there a way to add custom size class in order to do that ?
Or should I set auto-layout constraint programmaticaly depending on screen size ?
Thanks
At this point in time you really only have one solution, and it is using constraints.
Constraints have been existing for a long time now and should be used in all iOS projects. Never EVER change view sizes using their frames. Always adjust the constraint instead.
You can achieve pretty much anything using constraints. There are many tutorials / stack posts about it, you'll be able to look it up.
From what I understand here, you want your view to be bigger on bigger screens, and smaller on smaller screens. Without any other information i can only show you a very trivial example.
Add 4 constraints in storyboard (or in code, but it's just easier in storyboard I believe) from your resizing view to another view. Those four constraints should be Top, Bottom, Leading and Trailing (Up, Down, Left and Right).
Each constraint will basically say "my view should be X units from its superview, on that side".
If you go in the constraints attributes you can configure different things, like "I want my left side to be X units from the right side of that other view".
I suggest you mess around with colored empty views and see how they work, and if you can, have a more experienced programmer answer your questions on the side, because constraints, as simple as they are, are quite confusing at first. It took me about 2 months to be what i consider comfortable with it, and I'm a full time developer.
I can answer a couple more questions here if you want, but I'm pretty sure we'll be off topic really fast !
Have fun using constraints, they're great !

Xcode Constraints

I'm having a problem with Xcode constraints. I'm laying out my entire layout, then selecting all the items and having Xcode 'fill in missing constraints'. Now when I run the application in the simulator, labels and textboxes are NOT in the right places. Sometimes not visible at all (appear to be off screen)
So are there any tips or tricks? Should I layout 1 item at a time, set the constraints automatically then move to the next?
Don't trust XCode and 'fill in missing constraints' feature. The best option is to set all constraints manually. After some practice it's not that hard. You need to indicate X and Y coordinates and sometimes height/weight.
I really don't advice setting constraints automatically. It usually causes more problems than benefits.
Also check the console log at runtime. It may happen that there are conflicts between constraints and you need to fix it.
Check out the official guidlines

iOS / XCode Auto Layout Woes. Easier Way?

I'm wondering if xCode auto layout / constraints are really as frustrating as I think, or am I just not understanding them. For example, I started with this basic label in the view controller:
Fair enough. A box with text that has equal margins on the left/right and a smaller top margin. Now when I run any size device, that gets skewed/cut off from the device view. So right away for some reason Xcode thinks that despite me putting the entire label in the view controller, it things it should display halfway off the screen. Don't get it but okay, so I went ahead and added auto layout constraits to the right, left and top margins. The result is:
So it centered it, which is nice, but now it just ignores the fact that I made the width larger and it just shrinks it down anyways? For something so simple this seems to be very...unreliable. I then added the "aspect ratio" constraint and it seemed to look fine in all of the devices...finally!
I curiously also simply tried to get rid of all those, and simply add the "aspect ratio" constraint and the "horizontal center in container" constraint. On the main storyboard preview (not the simulator) it looked like this:
Not what I'm looking for, as it's not stretched downward like I want, they shrunk it again. However...when actually running it in the simulator...it looks perfect, the way I want it to in the first picture of the view controller.
Why the preview and simulator differed, I suppose (from reading other questions) it's because there were a few warnings after I added these. One was that the horizontal and vertical positions are one initially and will be different at run time. But when I ran it, it definitely held the initial ones and not the ones they said it would be at run time. It also told me that vertical position was ambiguous...well yeah I only set a center and aspect ratio...pretty obvious and not sure how I'd even solve that error. It solves it when I delete the horizontal center...but now it's not centered which is a step back from what I wanted.
Long story short, how do I deal with this. My previews and simulations aren't accurate with each other even for the simplest auto layout specifics. Apps nowadays are so complicated and on every device they scale and work beautifully, and this seems to not agree with even just one label.
Does anybody have any good articles, advice, or anything that would help me? This auto layout stuff seems to be so picky and that's really all I know so I have to deal with it. And don't even get me started on why the text doesn't scale with the label...why would I want the text on an iphone 4 to be the same on an ipad even when my label is increasing in size with the device?
A lot to go through, but it's just very frustrating and I can't see myself doing much else before I try to understand these basics. Thank you and much appreciated.
I think you have to understand constraints first...without that you always made things frustrating....For understand the constraints you can check this links
http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1
http://mathewsanders.com/designing-adaptive-layouts-for-iphone-6-plus/
It takes a bit of experience to get used to it, but once you get it, you get it for good and it's very easy.
Basically the rule of thumb is don't trust exactly what you see in storyboard when it comes to the size of the actual object. Say you have a UIView centered horizontally and vertically in the main view. True it will try to keep the size of that UIView but center it in any size device, but I would never just leave it at that. I would either:
Add width and height constraints
Add a width/height constraint and an aspect ratio constraint
Add a width constraint and top and bottom constraints
Add a height constraint and leading and trailing constraints
Add top, bottom, leading, and trailing constraints
In other words, the exact size of the object should always be determined via the constraints, and not by the UIView itself. This gets away from the finicky behavior and also ensures that it's displayed on any device exactly the way you intend it to.

iOS Simulator not displaying correctly in Xcode 6

I've been having such a hard time trying to figure out how this thing works. It's so random and I have no idea what else to try. I've looked up multiple articles on this issue and everyone just says change the scale. Changed the scale does not help, it's got nothing to do with what's happening here. I'm not sure if this is related to the bottom of Xcode where you can change the dimensions (Any vs Any / Any vs Regular Height, etc...) I've asked my mobile development teacher at school as well and he couldn't figure it out either. Any help would be greatly appreciated!!
Picture below:
http://tinypic.com/r/281fw5w/8
Your problem is not scaling. What you need to look at is auto layout and constraints.
You can use the icons in the lower right edge of the interface builder to get at them or control drag from a view controller (like a button, label, etc.) to the containing view (or any other view controller for that matter.) Usually, the main view window itself. When you release you can now add constraints to "attach" the element to that other element relatively. For instance, you could attach the things on the left to the left side and the things on the right to the right side. Now, regardless of the dimension of the actual device screen, those elements will appear in those locations relative to the device screen.
The problem is that the position of elements from your perspective is right for the canvas you see in the Interface builder, but once the app is run, the real canvas has different dimensions.
To manage the position, size and other attributes of UI elements, there is a system called AutoLayout.
It is quite ingenious because it is similar to natural language.
For example "I want this element to be in the middle of the screen."
or
"I want this element to be 20 pixels from the left corner and 57 pixels from the element that is above this element."
By combining these rules you basically create a set of layout constraints, that are applied in runtime to the view hierarchy and view are laid out properly.
Autolayout allows for very sophisticated layouts.
Another aspect you need to take into account that you might want your app to look well in all form factors from 3.5 inch iPhone up to iPad air.
Since these devices differ considerably in size, Apple introduced an abstraction called Size Class.
Size Class is an abstraction on top of concrete size. Concrete iOS devices have vey concrete dimensions. But in natural language you often say it's big ,or small ,or normal. And this the level of abstraction size classes use.
For each size class you can have a particular set of auto layout constraints.
So by combining AutoLayout and SizeClasses, Apple solved the problem oh how to have one application but one that can still accommodate specific form factors and can adjust its layout to them.
In Xcode6, all storyboards/xib files have autolayout & sizeclasses enabled by default. Interface builder provides you with a comfortable environment where you can set up your layout by creating constraints for each size class combination.

Adjust Center of Measure in Xcode 6

In the new "Xcode 6" the ability to change the center of measure of a button is missing.
In Xcode 5: http://imgur.com/jWHJp4v
Xcode 6: http://imgur.com/rsNayVZ
When I put an item (like a button or label) somewhere, I am unsure of the center, so my code that deals with the item is incorrect.
The "center" shown here in Xcode 6 is measured from the top left of the item, not the actual center.
How should I fix this so it is measured from the center like here, in Xcode 5?
You can't “fix it”. That control is gone in Xcode 6. I suspect they removed it to encourage you to use constraints instead of setting frames directly. You can use constraints to pin the center, right, or bottom of a view.
It's driving me crazy that the origin widget has been removed. It has made the placement of any object relative to another very difficult. I always use auto layout and I'm not sure how this improves it. Also, I'm not sure I follow the logic of improving a feature by making it more difficult to use. In the past, it was very helpful to set the "anchor point" of the origin and then adjust the object's position or size and have the frame grow on the unanchored sides. Now i have to break out the calculate and add position + size and then add buffer space to figure out where next object should start. And if I adjust the first object's frame in any way I need to break out the calculator again. Once you have a few objects that are relative to each other, it becomes vary painful to tweak any object's frame without having to recalculating everything.
As a side note, the removal of the ability to delete a constraint right in the size inspect is also a confusing choice. Instead we have to go through the document outline and manually delete them there. This is not a big deal for the size constraints as they are keep in the object's sub-directory however finding edge related constrains is much more difficult as they are grouped with the rest of the edge constrains and can be difficult to find. Again, is this an example of encouraging / improving auto layout? Very frustrating!
The feature has been removed from Xcode. I believe this is to account for the various sizes of devices now available, and as a result, the use of constraints is the way to go.
If you are using auto layout then this two will help you place you control in center for all the view different size of devices.
simple:
http://technet.weblineindia.com/mobile/using-auto-layout-in-xcode-6-for-universal-ios-app-development/
Advance:
http://www.youtube.com/watch?v=G53PuA_TlXk&feature=youtu.be&list=UUtc1Jt_UTPsXpAGtvlr0nUQ
Apple has removed this feature as there are 4 different size of devices only for iphone and it would be difficult to set frame for all controller and will increase the length of code and will become a tedious task as it check's the size of device and then set frame so in XCode 6.0.1 we need to use auto layout + size class and prepare our view based on the constraints set in storyboard or Xib.
Hope the above link's will help in solving your problem.
If your layout isn't too complex you can "Pin" your button or view to the top or bottom, sides, etc.
Or you can do your constraints manually here:
The first method works really well and is super easy! I had a similar problem as you looking for tools that have been left out in Xcode 6 and found this wonderful tutorial on Auto Layout:
http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1
If you are trying to center it I would click on the button or view you are working on and look at the size inspector here:
Look at where I circled and make sure that the bottom space and the top space are equal to the same amount (mine aren't equal), but that should give you the center.

Resources