Código para incluir en las vistas .xib view
class XXXXXXView: UIView {
@IBOutlet weak var view: UIView!
var customConstraints: [NSLayoutConstraint]!
//MARK: Init code
convenience init(frame: CGRect, q: ProjectQuestion) {
self.init(frame: frame);
}
override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
deinit {
// NSNotificationCenter.defaultCenter().removeObserver(self, name: UIDeviceOrientationDidChangeNotification, object: nil);
}
override func updateConstraints() {
self.removeConstraints(customConstraints)
self.customConstraints.removeAll()
if self.view != nil {
let views = dictionaryOfNames(arr: self.view)
//let views: [String : AnyObject] = ["": view]
customConstraints.append(contentsOf: NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: views))
customConstraints.append(contentsOf: NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: views))
self.addConstraints(customConstraints)
}
super.updateConstraints()
}
func dictionaryOfNames(arr:UIView...) -> Dictionary<String,UIView> {
var d = Dictionary<String,UIView>()
for (ix,v) in arr.enumerated(){
if(d.count==0){
d["view"] = v
}
else{
d["v\(ix+1)"] = v
}
}
return d
}
func commonInit() {
self.customConstraints = [NSLayoutConstraint]()
var view: UIView? = nil
let objects: [AnyObject] = Bundle.main.loadNibNamed(String(describing: type(of:self)), owner: self, options: nil)! as [AnyObject]
for object: AnyObject in objects {
if (object is UIView) {
view = object as? UIView
}
}
if view != nil {
self.view = view!
view!.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(view!)
self.setNeedsUpdateConstraints()
}
initValues()
}
func initValues() {
self.btnNo.setTitle("no".localized, for: UIControlState.normal);
self.btnNo.setTitle("no".localized, for: UIControlState.highlighted);
self.btnYes.setTitle("yes".localized, for: UIControlState.normal);
self.btnYes.setTitle("yes".localized, for: UIControlState.highlighted);
}
}
//SE DEBE ENLAZAR LA VIEW de esta property CON LA VIEW DEL XIB QUE ENGLOBA TODOS LOS COMPONENTES (mediante File's Owner)
// OJO!! Se debe poner la clase controladora del XIB en Files Owner!! NO EN LA VIEW!!
// Y LUEGO enlazar la view principal con la view por código del .m
@interface XXXXXXView ()
@property (strong, nonatomic) IBOutlet UIView *view;
@property (nonatomic) NSMutableArray* customConstraints;
@end
@implementation XXXXXXView
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder])) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
_customConstraints = [[NSMutableArray alloc] init];
UIView *view = nil;
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
owner:self
options:nil];
for (id object in objects) {
if ([object isKindOfClass:[UIView class]]) {
view = object;
break;
}
}
if (view != nil) {
_view = view;
view.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:view];
[self setNeedsUpdateConstraints];
}
}
- (void)updateConstraints
{
[self removeConstraints:self.customConstraints];
[self.customConstraints removeAllObjects];
if (self.view != nil) {
UIView *view = self.view;
NSDictionary *views = NSDictionaryOfVariableBindings(view);
[self.customConstraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat:
@"H:|[view]|" options:0 metrics:nil views:views]];
[self.customConstraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat:
@"V:|[view]|" options:0 metrics:nil views:views]];
[self addConstraints:self.customConstraints];
}
[super updateConstraints];
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}