Reputation: 850
I am using drawRect:
to draw shapes. I have to invoke this method and redraw the shapes when orientation changes, so i thought if there is a way to invoke drawRect:
automatically when layoutSubviews()
been called.
Thank you.
Upvotes: 1
Views: 472
Reputation: 80901
You can set the view's contentMode
to .redraw
.
This will invoke setNeedsDisplay
(and therefore drawRect
) whenever the view's bounds change, and will therefore also be invoked upon rotation (provided you've setup your autoresizingMask
so that the view's bounds change on rotation).
Upvotes: 6
Reputation: 855
In viewDidLoad() function put:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
and then add the following function:
func rotated()
{
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
print("landscape")
// Your drawRect function here
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
print("Portrait")
// Your drawRect function here
}
}
Upvotes: 0
Reputation: 2566
From the UIView class reference https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/drawRect:
This method is called when a view is first displayed or when an event occurs that invalidates a visible part of the view. You should never call this method directly yourself. To invalidate part of your view, and thus cause that portion to be redrawn, call the setNeedsDisplay or setNeedsDisplayInRect: method instead.
That's it. Just call setNeedsDisplay
on the appropriate view.
Upvotes: 2