UIView drawRect not called on autorotate - ios

I have a custom view that is added to the view controllers view
[self.view addSubview:myView];
In the view I added :
self.autoresizingMask = self.superview.autoresizingMask;
But my view's DrawRect method is not called when auto rotating... Why? I need the view's bounds to change so I can redraw it on auto rotate ...

What is the value of self.superview.autoresizingMask? That's probably why nothing's changing. Either explicitly set the autoresizing mask to what you want it to do, or if that doesn't provide enough control, use the ViewController's willAnimationRotationToInterfaceOrientation method to explicitly set the subview's frame or trigger a setNeedsDisplay: on the subview.

Related

iOS:use the frame constrained by Auto Layout to create a subview

I create a view called boundView using IB and Auto Layout,then in the controller I call [self.boundView layoutIfNeeded],then I pass self.boundView.frame.size to a method to generate the size of boundView's subview CardView. And then use the
PlayingCardView *playingCardView = [[PlayingCardView alloc]initWithFrame:frame];
to create subview programmatically. I do use NSLog to check that the size of subview is smaller than superview. But when I use [self.boundView addSubview:CardView] to add the subview. It is larger than superview!
Is there something wrong with the coordinate?Or it is because I combine the Auto Layout with the view I create by code?
Where are you doing this? If it is in viewDidLoad then the autolayout sizes will not have been calculated yet. Try doing it in viewDidLayoutSubviews.

Hide subviews when superview height change while using auto layout constraints

I have one UIView, it's having few subview controls like label's and textbox. it is also having one switch control.
I want to hide/display (like collapsible) the portion on the superview based on the switch change. However when I try to do it with constant of superview, it just changes the superview height but all subview does not getting hides.
could you please help me to figure out this.
Thanks,
Set UIView's property clipsToBounds = true.
This prevents the subviews from being drawn when their frame lies outside the bounds of the superview
e.g.
superView.clipsToBounds = true
Note that for layers you can use:
superView.layer.masksToBounds = true
There are a number of methods of UIView that allow you to modify the view hierarchy.
bringSubviewToFront:
sendSubviewToBack:
insertSubview:atIndex:
insertSubview:aboveSubview:
insertSubview:belowSubview:
exchangeSubviewAtIndex:withSubviewAtIndex:
You can use any of this as per your requirement.

Subclassed UIView not visible, however content is visible

I subclassed UIView: http://pastebin.com/MVk1XmPS and when I add it to another view of a UIViewController:
self.myView = MyView.newAutoLayoutView()
self.myView.backgroundColor = UIColor.redColor()
self.view.addSubview(self.myView)
the red background is missing. It seems that myView has no width and height. However the content is visible:
I have to hard code the dimension:
self.myView.autoSetDimensionsToSize(CGSize(width: 100, height: 100))
but shouldn't it grow automatically by the size of the content? I use swift, xcode6.1 and iOS8.1
Views do not automatically grow to fit their content. If you enable clipsToBounds on your view, you will see the content disappear. Alternatively, if you put a button as part of your content, you will see it but be unable to interact with it because it is outside of its superview's bounds.
In your subclass, you could implement the sizeToFit or sizeThatFits: methods, but these are not going to be automatically called by the system. You should consider assigning constraints for your view in your storyboard. If you're not using a storyboard, you will need to assign a frame in your viewWillLayoutSubviews or layoutSubviews method.
Storyboards will also like your custom view better if you implement intrinsicContentSize.

Can't set frame of a UIView while it's a child of UIScrollView

I have created one UIView inside the UIScrollView from storyboard.
I want to set the frame to UIView programatically. I have tried to set frame using setFrame function programatically, but its not working for the UIView which is inside UIScrollView.
if I create one UIView inside the UIScrollView using [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 110)],and than change size of the view,it is work!
my code:
but I want to create a uiview by IB and set frame using setFrame function.
Most impornatant condition is Autolayout is "TRUE".
Most impornatant condition is Autolayout is "TRUE".
If you want to mix constraints with setting a view's frame directly, you will need to make sure to set view.translatesAutoresizingMaskIntoConstraints = YES for your view. AFAIK, this cannot be done in IB for iOS, only in code.
Also beware that setting translatesAutoresizingMaskIntoConstraints = YES will add new constraints to those you define in IB (or programmatically) and you have to ensure they remain consistent, otherwise you will get an exception. So, e.g., if you set the frame origin in code, but also pinned the top distance between your view and the superview, you will run into problems if those specifications are not compatible.

How to set a subview's frame

I have a view hierarchy under a UIViewController that consists of a main view and a few subviews of the main view, all defined in a storyboard, and all connected using IBOutlet.
I would like to set the position and size of the subviews relative to the main view.
I've tried setting them in viewDidLoad and viewWillAppear. Neither work, and when called, the main view has valid frame values but the subviews all have frames of (0,0,0,0).
If I do nothing, the subviews appear exactly where positioned on the storyboard. I need to make the following code work, but I don't know where to put it.
CGRect newFrame = CGRectMake(10,10,50,50);
[subView setFrame:newFrame];
Another way to ask this question:
Where and when do the frames of storyboard subviews get set?
Or, should I just create all the subviews with code?
when using autolayout you can set frame of the views (if the frame is calculated from super view's frame) in viewDidLayoutSubviews method or the other way I use is calling this method [view setTranslatesAutoresizingMaskIntoConstraints:YES]; and then [view setFrame:...];. Hope this helps.
The position and size of subviews can be set when the UIViewController calls viewDidAppear but that causes the presentation to jump. Setting the position and size of a UITableView in viewDid/WillLayoutSubviews does not work.

Resources