I have a UIView whose frame I set to (0,60,768,944).
I find that if I set the autoresizing mask prior to adding it to another view, it fails to autoresize.
If I set it after the addition to the superview, that autoresizing works, but when the view shows up, it is - for some reason - has the dimensions 1536x1948.
So neither order is really working for me.
Any advice on what I'm doing wrong?
Many thanks in advance,
Sam
I have discovered that the right place to set the autoresizing mask is in viewDidAppear. By then (and only by then), all of the views have their final sizes.
Related
I believe using autoresizing for orientational changes would be good while managing the UI with Autolayout.
So many programmers are recommending against both at the same time,But As far as I understand it should be fine.
Because Autoresizingmask is easy to play with.
I think autoresizing mask itself turns into constraints actually I believe
translatesAutoresizingMaskIntoConstraints
Correct me If I am wrong
If its okay or not okay can some explain in depth why ?
As far as I am aware it is absolutely alright to use both autolayout and autoresizingmasks together. What you don't want to do is add autolayout constraints to a view that you are using autoresizing masks to govern layout. A general use case for autoresizing masks is adding a view to a view and wanting it to be pinned top, bottom,leading, and trailing. In that case it is simply
let pinnedToSuper = UIView(frame: self.view.bounds)
//all views default to .translatesAutoresizingMaskIntoConstraints if added programmatically
pinnedToSuper.autoresizingMask = [.flexibleWidth,.flexibleHeight]
self.view.addSubview(pinnedToSuper)
Notice how much easier this is as opposed to adding each constraint.
Prior to autolayout autoresizing masks were all iOS had to help with layout. Heads up autoresizing masks are also known as springs and struts. An example use case autoresizing masks break down is if you want a view to maintain a perfect square(or to make a circle) keeping aspect ratio and also resizing with the view in all orientations. In this case you would have to add code in layoutSubviews to resize the view manually based on parent bounds. You can see that this could get tedious especially if you are dodging views that are separately being handled by autolayout. This is probably why it is good to be careful when using both. I tend to use it in simple cases such as adding a view that sticks to the superviews bounds.
Important distinction when using together.
You should avoid trying to add autolayout constraints to a view that you are using autoresizing masks to attempt to blend them and achieve a layout because you will likely get conflicting constraints with no real effect. You can however add autolayout constraints to a view that has a subview that is being governed by autoresizing masks and there should not be any issues. This is my typical use case. I hope this helps you with how you might use it.
I started to create a very simple tic-tac-toe game.
The main goal is to make the view proportional to all screen sizes of all iOS devices.
So I put the image on a ViewController, make it full size of screen and then I put it into a Stack View. I've added constrains to this Stack View 0 to all sides.
And when I ran the simulator then everything looks good. But I receive a message in console panel. What does it mean?
It could happen because you changed the Mode attribute of the StackView:
I got this warning when I set Mode to Aspect Fit.
Any layer change on a UIStackView will result in this warning. As the warning points out UIStackView is a transform-only layer and it differs quite slightly from a UIView. As per documentation:
The UIStackView is a nonrendering subclass of UIView; that is, it does
not provide any user interface of its own. Instead, it just manages
the position and size of its arranged views. As a result, some
properties (like backgroundColor) have no effect on the stack view.
Similarly, you cannot override layerClass, drawRect:, or
drawLayer:inContext:.
More on this here.
It may not point to the exact source of the OP's issue but hopefully, it will shed some light on why UIStackView behaviour is not the same and should not be confused with UIView.
For someone that need: I set clipToBounds to false in StackView.
It looks like what you did is perfectly correct.
However, I'm wondering why you chose to put your imageview into a stackview when you could have just as easily pinned the sides without the stackview. I am a big fan of stackviews but they don't make sense everywhere. Unless you're planning on adding new views, and resizing your ticktactoe board, I might not use it.
I have a problem here. For last 3-4 days I'm trying to the find the solution but no avail. I'm using a xib to load a view as a subview to scrollView.
I'm using iOS8 and size classes and auto-layout as well.
The problem is that the view gets added to the scrollview but scrollview is unable to go beyond a point. That is I'm not able to scroll till the bottom.
This is frustrating as I have just started to develop iOS apps.
I'm attaching the screenshots for the constraints that I have applied. Hope to get some help here.
The first screenshot is the actual view controller's view that holds the scrollview.And the Place Detail Info View is the placeholder where the view loaded from xib is added. The screenshot also lists the different constraints as well.
The second and third screenshots is of the xib file which i'm trying to load into scrollView.
Please let me know where I have gone wrong.
As said above, the issue is related with the content size of your scroll view. Just try adding width and height (either explicit or implicit: see explanation below) constraints to every direct subview of your scroll view and then define all distance constraints among those.
* By "implicit" width/height constraints I mean things like "aspect ratio", "equal width/height" relation between two views.
P.S. When dealing with scroll view-like controls and auto layout, there should be an exhaustive (some of which may seem redundant) set of constraints for subviews.
Hope this helps.
Your problem is with the view who is immediate subview of your scrollview.In autolayout,scrollView calculate its content size according to its subview.
follow these steps after removing all constarints from this view,i hope u will get your problem solved.
1.for your scroll view,set all 4 constraints i.e leading edge,trailing edge,superview and bottom constraints.
2.now for the view that is immediate subview to your scrollview,set all 4 constraints (leading,trailing,top and bottom),along with these set height constraints and make width equals to scrollview.
if your immediate subview's height is greater than your screen size or scrollview,it will scroll for sure.
run and check whether you are able to scroll and proceed furthur.
after step 2,all your constarints related ambgious type error will resoved.now you can set all other constraints as usual.
for more clear understanding setting constarints in scrollview see this https://www.youtube.com/watch?v=4oCWxHLBQ-A
In interface builder itself there is an option to preview your constraints for all the available devices. This will ensure you that the constraints work fine for all the available devices from Apple. Here is a quick tutorial that will help you with it.
http://adoptioncurve.net/archives/2014/08/working-with-size-classes-in-interface-builder/
Once that is verified and you are still not able to find a suitable solution, then probably there is something wrong in the content size for your scroll view. Try to increasing your scroll view content size.
I hope this should solve your problem.
Happy coding. Cheers :)
when I use old AutosizingMask, I got this
but when switch to new AutoLayout, it seems that I must set an/muti edge of screen, but actually I need none of them.
Why I need this is I want to use just one storyboard to adapt both iphone3.5/4 and ipad,
is there any solution to solve this or achieve my purpose?
thx :)
If I am understanding correctly you want the red square to grow if the container grows.
So for that you would add fixed user constraints to the top, left, bottom and top of a fixed value.
I would recommend using a different Storyboard for the iPad thought because you could preview the changes is Interface Builder.
I have a custom UIview
I need to increase its width and also the width of its subviews like webviews and another label etc. dynamically.
I have used
_view.setframe - xframe;// this increases the width but the content stays where it is
_view setneedsdisplay or needslayout are not working.
Do I use the autoresizing mask before adding the view in controller? and how?
Can someone please tell me how to go about doing this.
Thanks
You need to set autoResizesSubviews on the parent to YES, then set the autoresizingmask for each subview. Here is the documentation for autoresizingmask
https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/CreatingViews/CreatingViews.html#//apple_ref/doc/uid/TP40009503-CH5-SW5
This image from apple should help you understand:
Thanks for your inputs
but i worked by recalling
[self layoutSubviews];//and changing the widths