I have a UIView that is sized to fit the entire screen minus 5 pixels on each side. I have set it to autoresize however this makes the view lose its 5 pixels on one side, and completely fills the sides when flipped horizontally. I need the 5 pixel buffer to exist in all orientations. Is there any way I can tweak the way auto resize works to make this happen?
My code:
[newsfeed setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
Also, I have dragged one of the many views in from storyboard, and that one does not respond to the code above. How can I make it auto resize?
When you also specify that the top and bottom margin are flexible, you in effect say that the left and right margins are fixed. Try this:
[newsfeed setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
As others have commented, this is more easily done in Interface Builder with springs and struts:
Related
I am working on app and I am in trying to support my app for iPhone 4,5,6 and 6plus with one view. I chose to Autoresize for this operation, I am not going in discussion why I didn't chose Autolayout.
Now I have a Save button with Autoresizing mask set as shown in picture flexible of all corner. and somewhere after I need to change Save button position via code after setting updated frame of Save button, its corner flexibility change to static and this stick on all devices what I added new frames, and this looks quite odd in 6plus and 4.
Here below is my code to update frame.
[self.saveProfileButton setFrame:CGRectMake(self.saveProfileButton.frame.origin.x, PIOYPositionOfSaveButtonForEditingProfile, self.saveProfileButton.frame.size.width, self.saveProfileButton.frame.size.height)];
self.view.autoresizesSubviews = YES;
self.saveProfileButton.autoresizingMask = (UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleTopMargin);
I am trying to reset these flexible margins because of this goes to static.
I am looking for help how to make this flexible margins after updating this button frames.
I'm developing an app targetting iOS6/7, and I've lost two hours staring at a storyboard, trying to understand why autolayout doesn't do what I want it to do.
Basically, I have a scene containing a scroll view and in it, I want to have a UIIMage anchored at the bottom right. Therefore, I set four constraints for the image:
Width equals
Height equals
Bottom space to superview and
Trailing space to superview
XCode does not complain about the positioning, so I run my app with confidence, only to find that it is not shown in any orientation. It's just nowhere to be found!
I know that to find how autolayout implemented the constraints and did its magic, I have to inspect the view.bounds rect. I checked at the viewDidAppear event, to find its value to be as expected though:
Image pos is: 0.000000,0.000000 106.000000x103.000000
The frame is of course at the actual position in the storyboard which I guess is to be expected.
Here is a screen cap of my Storyboard:
Any ideas?
Update:
Some more info:
If I remove all constraints and run the app, the image view is shown at the bottom right of my view in portrait, but when rotating, as expected, it is not shown.
Update 2:
This all should fall in the dreaded UIScrollView and AutoLayout threads. In the end I reverted to using a UIView, inside of which is a UIScrollView containing all the content I wanted to scroll (so that no text fields are hidden by the keyboard in landscape mode). The image I wanted anchored at the bottom was left at the container UIView and it all worked as intended.
If you want to anchor to bottom right, Programmatic autoresizingMask has been far more consistent for me.
It's slightly opposite of IB so if you want to anchor bottom right, you want the left margin flexible and the top margin flexible
yourView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
This means that bottom margin, right margin, height and width will all remain the same, keeping it in the lower right of your view.
I am trying to use springs and struts to automatically resize content. What would the best way to create constaints like this, or is there a better way targeting iOS5+?
[View 1 - fixed height]
(10 pixel padding)
[View 2 - variable height scrollview / tableview, contains content]
You are confusing the issue by referring to “autoresizing constraints”, because “autoresizing” usually refers to the old “springs and struts” system for updating view layout, and constraints are part of the new “autolayout” system (which requires iOS 6).
Anyway, you want to set view 1's autoresizingMask to UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin. This will keep its top, left and right edges pinned relative to its superview, and will keep its height fixed. If you're laying it out in Interface Builder, set it like this:
You want to set view 2's autoresizingMask to UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight. This will keep all of its edges pinned relative to its superview, and will let it get taller/shorter (and wider/narrower) in sync with changes to its superview's size. If you're laying it out in Interface Builder, set it like this:
It's up to you to lay them out with a 10 point space between them. If you do that, and set the autoresizing masks as I have described, then they will keep the 10 point space automatically when the superview changes size.
I have an image view and underneath it a webview.
They have a small vertical distance between them when viewed on a 3.5 inch screen but when I apply retina 4 Form Factor the image view expands (the actual image size is fixed) so that there appears to be a big gap between it and the webview underneath. Also there is a gap between the image view and the top of the screen.
I have experimented with constraints but don't seem to be able to alter things so that it is the webview which expands vertically, leaving a small gap between it and the imageview.
You need to set UIImageView autosizing property:
OR you can set it programatically using
imageView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
If this doesn't solve your problem, you have two possible solutions for this. Listed below:
1.In the 'Simulated Metrics' section of UIVIew's attributes inspector, you can choose between 3,5" or 4" sizes. Choose 3,5", and make your views and subviews resizable .This will make it automatically scale your view to fit iPhone 5's screen.
2.OR you can make use of Auto Layout (constraints) with the deployment target of iOS 6+.
Hope this will solve the issue.
I want to position a sub UIView (sub_UIView) as a subview of parent UIView (parent_UIView). When the parent_UIView resizing/moving, the sub_UIView would stay the relative position of the parent_UIView. For example, the sub_UIView is at the bottom right corner of the parent_UIView. When the parent_UIView is moving, the sub_UIView would stay at the bottom right corner of the parent_UIView automatically.
I managed to do this manually by updating the frame of the sub_UIView when the parent_UIView's frame moving, but how to do this automatically in iOS? are there any properties for this? (similar to autoresizingmask for resizing subviews)
Updated old response, that only mentioned resizing masks
Autolayout (iOS 6)
In iOS 6, autolayout was added, albeit, kinda ugly to work with XCode in a storyboard/xib. Autolayout is way too big to explain, but the essence of it, is that it's a set of rules between views within the hierarchy. Thus, you can stick the x position of a view to the right border of a parent view. See the Auto Layout Programming Guide
Autoresizing Masks (iOS 2)
Take a look at the options in the Size Inspector:
This is the equivalent of doing
myView.autoresizingMask = UIViewAutoresizingMaskFlexibleTopMargin |
UIViewAutoresizingMaskFlexibleLeftMargin |
UIViewAutoresizingMaskFlexibleWidth |
UIViewAutoresizingMaskFlexibleHeight;
Notice that there's a difference between doing it by code and doing it through IB. The IB autoresizing settings for the borders work as struts, selecting the right one, for example, means, "my right border is now stuck to the superview's right border".
On the other hand, code does the opposite, where you need to specify which borders are not strut-ed, aka. they are flexible. Height and Width works normally.
Layout Subviews (iOS 2, but gotchas for iOS5+)
If everything fails, don't start doing manual positioning all over the place, I've been in that position, and it just leads to unmaintanable, spaghetti code, where three or more methods are messing with the same view properties.
Instead, prefer to do all your custom positioning on UIView -layoutSubviews. This method gets called as needed when a setNeedsLayout has been triggered, for example, when the width a view changes, the subviews get the layout subviews event. Also makes implementing rotating interfaces much easier, since this method gets called for the new width, to determine how the animation will look like.
Keep in mind that layout subviews works after Auto Layout/Auto-resizing masks have been performed.