我有一个奇怪的问题,当我更改视图的背景时,我再也看不到作为该视图子视图的UILabel。我无法转储计算器应用程序的标题,因为它在Swift中,我不知道UILabel的名称是什么,而FLEXing对此没有帮助。我想将子视图放在背景视图前面(有两个子视图),但我目前的两种方法都会使目标应用程序崩溃。我试图看看我是否可以在最前面只使用一个。
我的代码:
NSMutableArray *labels = [NSMutableArray arrayWithArray:((DisplayView*)self).allSubviews];
if ([labels count] > 0) {
UILabel *mainLabel = labels[0];
[self bringSubviewToFront:mainLabel];
}
((DisplayView*)self).allSubviews = labels;
我也尝试过:
for (UILabel *label in ((DisplayView*)self).allSubviews) {
if ([label isKindOfClass:[UILabel class]]) {
[self bringSubviewToFront:label];
}
我需要一种替代方法来实现这一目标,一种方法可以使所有子视图前进,或者有人告诉我我做错了什么。编辑,这是我的完整代码:
@interface DisplayView
@property (nonatomic, copy, readwrite) UIColor *backgroundColor;
@property (nonatomic, assign, readwrite, getter=isHidden) BOOL hidden;
@property (nonatomic, assign, readwrite, getter=isOpaque) BOOL opaque;
@property (nonatomic, assign, readwrite) CGFloat alpha;
@property (nonatomic, copy, readwrite) NSArray *allSubviews;
@property (nonatomic, assign, readwrite) CALayer *layer;
@end
%hook DisplayView
-(void)layoutSubviews {
((DisplayView*)self).opaque = NO;
((DisplayView*)self).backgroundColor = [UIColor blackColor];
((DisplayView*)self).hidden = NO;
((DisplayView*)self).alpha = 1.0;
NSMutableArray *labels = [NSMutableArray arrayWithArray:((DisplayView*)self).layer.sublayers];
if ([labels count] > 0) {
UILabel *mainLabel = labels[0];
[self bringSubviewToFront:mainLabel];
}
}
%end
%ctor {
%init(DisplayView=objc_getClass("Calculator.DisplayView"));
}
似乎有几个可能的问题。首先,在allSubviews
上没有UIView
,它可能应该是subviews
。
NSMutableArray *labels = [NSMutableArray arrayWithArray:((DisplayView*)self).subviews];
if ([labels count] > 0) {
UILabel *mainLabel = labels[0];
(只是想指出你似乎没有改变数组,所以为什么要复制它?你可以直接访问self.subviews
)
第二,在CALayer
版本中,你将CALayer
投射到UIView
:
NSMutableArray *labels = [NSMutableArray arrayWithArray:((DisplayView*)self).layer.sublayers];
CALayer *mainLabel = labels.firstObject;
if (mainLabel) {
[labels removeObjectAtIndex:0];
[labels addObject:mainLabel];
self.layer.sublayers = labels;
}
也许其中一些有帮助