gpt4 book ai didi

ios - 用于自定义 UIVIew 的 UI_APPEARANCE_SELECTOR

转载 作者:行者123 更新时间:2023-11-28 22:05:18 24 4
gpt4 key购买 nike

我正在尝试为我的自定义类应用 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 中设置了我的外观。我将我的 View 添加到 Storyboard并分配了 MyCustomView 类。在 viewDidLoad 中,我从上面的代码中调用了 doSomething 方法,令人惊讶的是两个变量都是 nil。另一个奇怪的事情是 indicatorImageView 包含我在 AppDelegate 中设置的图像。最后,我在 drawRect 中将两个变量设置为正确的值。

问题是:我做错了什么?如何在 doSomething 方法中获得正确的值?

最佳答案

发生这种情况是因为外观是在 viewDidLoad 方法之后设置的。当调用 View Controller 上的 viewDidLoad 方法时, View 已加载,但尚未添加到 View 层次结构中。

如果您在 setMyColor: 方法中放置一个断点并打印回溯,您将看到:

  • setMyColor: 方法在 Controller 上的 viewDidLoad 之后被调用
  • 外观在 addSubview 方法之后应用,这总是在父 Controller 上的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com