English 中文(简体)
我何时可以使用UIAppearance 的属性设置开始使用?
原标题:When can I start using properties set using UIAppearance?

在我查看类中我有一些自定义外观属性( UIView 的子孙 ) 。 我想根据这些属性定制外观, 但我不能在初始化器中这样做, 因为使用 [[MyClass appress] setFoo:...] 设定的数值当时无效 :

@interface View : UIView
@property(strong) UIColor *someColor UI_APPEARANCE_SELECTOR;
@end

@implementation View
@synthesize someColor;

// Somewhere in other code before the initializer is called:
// [[View appearance] setSomeColor:[UIColor blackColor]];

- (id) initWithFrame: (CGRect) frame
{
    self = [super initWithFrame:frame];
    NSLog(@"%@", someColor); // nil
    return self;
}

@end

它们已经设置在 layoutSubview 中, 但这不是执行视图定制的好点, 因为有些定制可能再次触发 layoutSubview , 导致无尽循环 。

那么,什么是完成定制的好点? 还是有办法触发应用外观价值的代码?

最佳回答

一种可能的变通办法是直接从代理人那里获取价值:

- (id) initWithFrame: (CGRect) frame
{
    self = [super initWithFrame:frame];
    NSLog(@"%@", [[View appearance] someColor); // not nil
    return self;
}

当然,这排除了根据视窗容器改变外观的选择,而且一般是丑陋的。

- (void) setSomeColor: (UIColor*) newColor
{
    someColor = newColor;
    // do whatever is needed
}

我宁愿在外观属性设定后,

问题回答

为什么不等到

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];

    if (newSuperview) {
        ... code here ...
    }
}

如果它给你带来麻烦?

我相信 UIAppearance 属性在被添加到视图等级结构时被应用到视图中。 因此您可以访问 UIView didMoveToSuperview 中设定的属性 。

Caveat: 我使用Swift 2, 所以不能确定 Swift / object- C 的早期版本。 但我发现 < code> didd' diddmoveToSuperview () 将无法工作。 这些属性可以在 layoutSubviews () 中找到, 但这不是做类似事情的好地方( 因为它可以被多次调用 ) 。 在我发现的生命圈中访问这些属性的最佳位置是 didMove To Window ()

我本以为这个观点如果是一次性的,就最好了。否则,就看看WillAppear。

编辑:

如果您想在视图中这样做, 而不是它控制器, 那么我会为视图创建一个自定义信头, 其写法大致如下: :

-(id) initWithFrame:(CGRect) frame andAppearanceColor:(UIColor)theColor;

从而在创建时间将颜色传递到视图中。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签