Avoid putting setTranslatesAutoresizingMaskIntoConstraints (false) everywhere
I was wondering, now with Auto Layout you have to tell everyone UIView
that you don't want to translate your (legacy) auto-resizing masks into constraint layouts like this:
let view = MyView()
view.setTranslatesAutoresizingMaskIntoConstraints(false)
In my application, I have (almost) all of my views with custom constraints, so I never want to auto-translate auto-resizing masks. Wouldn't it be nice if the default setting for this would be false
? So, just in situations where I need to convert the autosize masks, I set it to true
?
Is there a way to make false
the default for setTranslatesAutoresizingMaskIntoConstraints
? Maybe with a clever extension (aka) of some kind?
source to share
I totally agree, so I made this category that includes an initializer like this:
extension UIView {
// MARK: Initializing a View Object
/**
* @name Initializing a View Object
*/
/**
* Returns a frameless view that does not automatically use autoresizing (for use in autolayouts).
*
* @return A frameless view that does not automatically use autoresizing (for use in autolayouts).
*/
class func autoLayoutView() -> Self {
let view = self()
view.setTranslatesAutoresizingMaskIntoConstraints(false)
return view
}
}
There is also a branch in the project, but this is a very early stage. We invite you to welcome!
source to share
Why not a category in UIView?
Title:
@interface UIView( MaskKiller )
- ( instancetype )myInit;
implementation:
@implementation UIView( MaskKiller )
- ( instancetype )myInit
{
if( self = [ self init ] )
{
self.translatesAutoresizingMaskIntoConstraints = NO;
}
return self;
}
Edit: A bit quick here, of course, doesn't solve anything when working with UIView subclasses. In this case, swizzling might be an option.
source to share