今天花了一下午的時間解決iOS的適配問題,本來使用了autolayout好好的,一個新同事拿到ios6.1上去一測,導航條和view中間空了一大截。
著了好多辦法,最后發現蘋果公司對iOS7.0專門設置了一個屬性:
/*
 New behavior on iOS 7.
 Default is YES.
 You may force an opaque background by setting the property to NO.
 If the navigation bar has a custom background image, the default is inferred 
 from the alpha values of the image—YES if it has any pixel with alpha < 1.0
 If you send setTranslucent:YES to a bar with an opaque custom background image
 it will apply a system opacity less than 1.0 to the image.
 If you send setTranslucent:NO to a bar with a translucent custom background image
 it will provide an opaque background for the image using the bar's barTintColor if defined, or black
 for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
 
*/
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0); // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent

好吧,問題就在這里。在代碼中做一下系統版本判斷,然后設置這個屬性為NO。

一切就好了,哎,浪費時間??!如果不改它,autolayout就會和你做對的。各位珍重.
//繼承UINavigationController,加入一個實現
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //適配ios7
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 7.0)){
        self.navigationBar.translucent = NO;
    }
}