這聽起來像當你設置bg圖像時,你實際上並沒有在rootViewController上設置它。或者,如果你是,那麼當它做一個viewDidUnload,然後viewDidLoad,bg圖像不會被保留。
您應該將用戶選擇的圖像放在rootViewController可以在調用viewDidLoad方法時訪問它的位置,並在調用此方法時重新設置它。
下面是查看給定視圖的子視圖的一些方便代碼,以便您可以查看視圖內實際存在的內容。
跌落下面的代碼到一個名爲InspectView新類,並調用它像這樣:
[InspectView dumpViewToLog:rootViewController.view];
這裏的類:
InspectView.m
#import "InspectView.h"
#define THE_LOG NSLog
@implementation InspectView
+ (void)dumpViewToLog:(id)viewObj {
THE_LOG(@"%@", [self dumpViewToString: viewObj]);
}
+ (NSString *)dumpViewToString:(id)viewObj {
NSString *s = @"\nInspect view hierarchy -----------------------------------" ;
s = [ s stringByAppendingFormat: @"\n original view is (0x%x)", viewObj];
// go up to outtermost view.
while ([viewObj superview]) {
viewObj = [viewObj superview];
}
s = [ s stringByAppendingString:[self dumpViewToString: viewObj level:0] ];
s = [ s stringByAppendingString: @"\nEnd of view hierarchy -----------------------------------"];
return s;
}
+ (NSString *) dumpViewToString:(id)viewObj level:(int) level {
NSString * [email protected]"\n";
// indent to show the current level
for (int i = 0; i < level; i++) {
s = [s stringByAppendingString:@". "];
}
s = [s stringByAppendingFormat:@"%@ (0x%x): frame:(%f,%f) %f x %f [tag=%d] ", [[viewObj class] description], viewObj,
((UIView*)viewObj).frame.origin.x,
((UIView*)viewObj).frame.origin.y,
((UIView*)viewObj).frame.size.width,
((UIView*)viewObj).frame.size.height,
((UIView*)viewObj).tag
]; // shows the hex address of input view.
// s = [s stringByAppendingFormat:@"%@ : ", [[viewObj class] description] ];
id obj = [viewObj superclass];
while (NULL != obj) {
s = [s stringByAppendingFormat: @"%@ : ", [[obj class] description] ];
obj = [obj superclass];
}
// recurse for all subviews
for (UIView *sub in [viewObj subviews]) {
s= [s stringByAppendingString: [self dumpViewToString:sub level:(level + 1)]];
}
return s;
}
@end
InspectView。 h
#define objectString(anObject) [[anObject description] UTF8String]
#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
@interface InspectView : NSObject {}
+ (void) dumpViewToLog:(id)viewObj ;
+ (NSString *)dumpViewToString:(id)viewObj;
+ (NSString *)dumpViewToString:(id)viewObj level:(int)level;
@end
感謝您的理解和一個不錯的代碼片段,但除了我的信息給,我也想提一下,我正在改變UIButton'[myBtn setBackgroundImage:img forState:UIStateNormal'的背景。請在這種情況下引導我。謝謝。 – Wasim
忽略我以前的評論。你的建議代碼像一個魅力:)感謝噸..!我在解決這個問題後面臨更多的問題,很快會回覆給你:) – Wasim