我正在尝试为自定义课程申请UI_APPEARANCE_SELECTOR
MyCustomView.h
@interface MyCustomView : UIView
@property (nonatomic, strong) UIImage *indicatorImage UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *myColor UI_APPEARANCE_SELECTOR;
- (void) doSomething;
@end
MyCustomView.m
@interface MyCustomView()
@property (nonatomic, strong) UIImageView *indicatorImageView;
@end
@implementation MyCustomView
-(void)initialize {
[self addSubview:self.indicatorImageView];
}
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initialize];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if(self) {
[self initialize];
}
return self;
}
- (void) doSomething {
// here indicatorImage and myColor are nil!
}
- (void) setIndicatorImage:(UIImage *)indicatorImage {
_indicatorImage = indicatorImage;
self.indicatorImageView.image = indicatorImage;
[self.indicatorImageView setNeedsLayout];
}
- (UIImageView *)indicatorImageView
{
if (!_indicatorImageView) {
_indicatorImageView = [[UIImageView alloc] initWithFrame:self.bounds];
_indicatorImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
return _indicatorImageView;
}
- (void)drawRect:(CGRect)rect {
// here indicatorImage and myColor are NOT nil!
}
@end
我在AppDelegate中设置外观。我将视图添加到情节提要中并分配了
MyCustomView
类。在viewDidLoad
中,我从上面的代码调用doSomething
方法,令人惊讶的是,两个变量均为nil。另一个奇怪的事情是indicatorImageView
包含我在AppDelegate中设置的图像。最后,我在drawRect
中将两个变量都设置为正确的值。问题是:我做错了什么?如何在
doSomething
方法中获得正确的值? 最佳答案
这是因为外观是在viewDidLoad方法之后设置的。
调用视图控制器上的viewDidLoad方法时,该视图已被加载,但尚未添加到视图层次结构中。
如果将断点放在setMyColor:
方法中并打印回溯,则会看到:
在控制器上在setMyColor:
之后调用viewDidLoad
方法
外观是在addSubview
方法之后应用的,该方法总是在父控制器上的viewDidLoad
方法之后发生。
看第8帧,_applyAppearanceInvocations
frame #1: 0x00d8d18d CoreFoundation`__invoking___ + 29
frame #2: 0x00eadb92 CoreFoundation`-[NSInvocation invokeUsingIMP:] + 242
frame #3: 0x0186364b UIKit`__workaround10030904InvokeWithTarget_block_invoke + 95
frame #4: 0x012be830 UIKit`+[UIView _performCustomizableAppearanceModifications:] + 29
frame #5: 0x018635e1 UIKit`workaround10030904InvokeWithTarget + 1047
frame #6: 0x0185d2f8 UIKit`+[_UIAppearance _applyInvocationsTo:window:matchingSelector:] + 4107
frame #7: 0x0185db14 UIKit`+[_UIAppearance _applyInvocationsTo:window:] + 56
frame #8: 0x012d7b1b UIKit`-[UIView(Internal) _applyAppearanceInvocations] + 287
frame #9: 0x012d85a4 UIKit`__88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 65
frame #10: 0x012d8531 UIKit`-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:] + 204
frame #11: 0x012d845f UIKit`-[UIView(Internal) _didChangeFromIdiom:onScreen:traverseHierarchy:] + 53
frame #12: 0x012d8422 UIKit`-[UIView(Internal) _didChangeFromIdiomOnScreen:traverseHierarchy:] + 172
frame #13: 0x012d79cd UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 1722
frame #14: 0x012d7666 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 851
frame #15: 0x012cea57 UIKit`__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 154
frame #16: 0x012ce9b5 UIKit`-[UIView(Hierarchy) _postMovedFromSuperview:] + 458
frame #17: 0x012da428 UIKit`-[UIView(Internal) _addSubview:positioned:relativeTo:] + 1943
frame #18: 0x012ccdbd UIKit`-[UIView(Hierarchy) addSubview:] + 56
关于ios - 自定义UIVIew的UI_APPEARANCE_SELECTOR,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24179625/