Skip to content

Instantly share code, notes, and snippets.

@mmassaki
Created September 26, 2012 20:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmassaki/3790270 to your computer and use it in GitHub Desktop.
Save mmassaki/3790270 to your computer and use it in GitHub Desktop.
UIView+NibLoading
- (id) awakeAfterUsingCoder:(NSCoder*)aDecoder
{
BOOL isJustAPlaceholder = ([[self subviews] count] == 0);
if (isJustAPlaceholder) {
CustomView *theRealThing = (CustomView *)[CustomView loadInstanceFromNib];
[self copyUIPropertiesTo:theRealThing];
// // convince ARC that we're legit
// CFRelease((__bridge const void*)self);
// CFRetain((__bridge const void*)theRealThing);
return theRealThing;
}
return self;
}
//
// UIView+NibLoading.h
//
// Created by Massaki on 9/24/12.
// Copyright (c) 2012 I.ndigo. All rights reserved.
//
// Based on http://stackoverflow.com/a/10934581
#import <UIKit/UIKit.h>
@interface UIView (NibLoading)
+ (UIView *)loadInstanceFromNib;
- (void)copyUIPropertiesTo:(UIView *)view;
@end
//
// UIView+NibLoading.m
//
// Created by Massaki on 9/24/12.
// Copyright (c) 2012 I.ndigo. All rights reserved.
//
// Based on http://stackoverflow.com/a/10934581
#import "UIView+NibLoading.h"
@implementation UIView (NibLoading)
+ (UIView *)loadInstanceFromNib
{
UIView *result = nil;
NSArray *elements = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil];
for (id anObject in elements)
{
if ([anObject isKindOfClass:[self class]])
{
result = anObject;
break;
}
}
return result;
}
- (void)copyUIPropertiesTo:(UIView *)view
{
// pass properties through
view.frame = self.frame;
view.autoresizingMask = self.autoresizingMask;
view.alpha = self.alpha;
view.hidden = self.hidden;
// // reflection did not work to get those lists, so I hardcoded them
// // any suggestions are welcome here
//
// NSArray *properties =
// [NSArray arrayWithObjects: @"frame",@"bounds", @"center", @"transform", @"contentScaleFactor", @"multipleTouchEnabled", @"exclusiveTouch", @"autoresizesSubviews", @"autoresizingMask", @"clipsToBounds", @"backgroundColor", @"alpha", @"opaque", @"clearsContextBeforeDrawing", @"hidden", @"contentMode", @"contentStretch", nil];
//
// // some getters have 'is' prefix
// NSArray *getters =
// [NSArray arrayWithObjects: @"frame", @"bounds", @"center", @"transform", @"contentScaleFactor", @"isMultipleTouchEnabled", @"isExclusiveTouch", @"autoresizesSubviews", @"autoresizingMask", @"clipsToBounds", @"backgroundColor", @"alpha", @"isOpaque", @"clearsContextBeforeDrawing", @"isHidden", @"contentMode", @"contentStretch", nil];
//
// for (int i=0; i<[properties count]; i++)
// {
// NSString * propertyName = [properties objectAtIndex:i];
// NSString * getter = [getters objectAtIndex:i];
//
// SEL getPropertySelector = NSSelectorFromString(getter);
//
// NSString *setterSelectorName =
// [propertyName stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[propertyName substringToIndex:1] capitalizedString]];
//
// setterSelectorName = [NSString stringWithFormat:@"set%@:", setterSelectorName];
//
// SEL setPropertySelector = NSSelectorFromString(setterSelectorName);
//
// if ([self respondsToSelector:getPropertySelector] && [view respondsToSelector:setPropertySelector])
// {
// NSObject * propertyValue = [self valueForKey:propertyName];
//
// [view setValue:propertyValue forKey:propertyName];
// }
// }
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment