1、IOS8適配遇到的問題
1、不能定位
打勾 設置- 隱私-定位服務-你的app-使用利用程序期間(始終)
打開app再進設置后會發現,你打勾的使用程序期間(始終)又給取消了
原來iOS8需要1些方法。
如果需要僅在前臺定位,你在調用startUpdatingLocation 前需要調用requestWhenInUseAuthorization
如果需要在前后臺定位,你在調用startUpdatingLocation 前需要調用requestAlwaysAuthorization
同 時在plist文件中添加NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription字段,值寫"需要定位"就能夠了,也能夠是其他的,這個提示文字"需要定位"在詢問用戶授權的時候會顯示到的。
1.if ([[[UIDevice currentDevice] systemVersion] floatValue]>= 8.0) {
2.[_locationManager requestWhenInUseAuthorization];
3.}
4.
5.[_locationManager startUpdatingLocation];
2、推送不管用
01.if ([[[UIDevice currentDevice] systemVersion] floatValue]>= 8.0) {
02.[app registerForRemoteNotifications];
03.
04.UIUserNotificationSettings *settings =[UIUserNotificationSettings settingsForTypes:
05.UIRemoteNotificationTypeAlert
06.| UIRemoteNotificationTypeBadge
07.| UIRemoteNotificationTypeSoundcategories:nil];
08.[appregisterUserNotificationSettings:settings];
09.
10.} else {
11.[app registerForRemoteNotificationTypes:
12.UIRemoteNotificationTypeAlert
13.| UIRemoteNotificationTypeBadge
14.| UIRemoteNotificationTypeSound];
15.}
ios8注冊推送分兩步走,
1》注冊用戶通知
UIUserNotificationSettings *settings =[UIUserNotificationSettings settingsForTypes:
UIRemoteNotificationTypeAlert
| UIRemoteNotificationTypeBadge
| UIRemoteNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
2》用戶通知注冊成功后,會走以下回調,在回調函數中進行推送注冊:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
- (void)application:(UIApplication *)applicationdidRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings
{
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
#endif
3》接下來就走之前的推送回調:
- (void)application:(UIApplication *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken //成功回調
- (void)application:(UIApplication*)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError*)error //失敗回調
判斷推送打開開關,ios8之前的代碼:
[[UIApplication sharedApplication]enabledRemoteNotificationTypes]!=UIRemoteNotificationTypeNone;
ios8需要換成以下代碼:
[[UIApplication sharedApplication]isRegisteredForRemoteNotifications]
3、某些系統控件的布局計算延遲到繪制時才產生:
舉個例子:
UIButton * button = [UIButtonbuttonWithType:UIButtonTypeCustom];
[buttonsetTag:i];
[buttonsetTitle:siteData.info.title
forState:UIControlStateNormal];
[button.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[buttonsetTitleColor:[UIColor colorWithHexValue:0x000000]
forState:UIControlStateNormal];
[buttonsetTitleColor:[UIColor colorWithHexValue:0xffffff]
forState:UIControlStateHighlighted];
[buttonsetTitleColor:[UIColor colorWithHexValue:0xffffff]
forState:UIControlStateSelected];
[button.titleLabel setFont:[UIFont systemFontOfSize:14]];
[button setBackgroundColor:[UIColorclearColor]];
UIImage*image = [UIImage imageNamed:@"sourceSelect"];
image =[image resizableImageWithCapInsets:UIEdgeInsetsMake(4, 2, 4, 2)];
[buttonsetBackgroundImage:nil
forState:UIControlStateNormal];
[buttonsetBackgroundImage:image
forState:UIControlStateHighlighted];
[buttonsetBackgroundImage:image
forState:UIControlStateSelected];
button.exclusiveTouch = YES;
button.selected = NO;
UIImage*tmpImg = [Utility ImageFromColor:[UIColor clearColor]
rect:CGRectMake(0, 0, 47, 60)];
[buttonsetImageWithURL:[NSURL URLWithString:siteData.info.logoURL]
placeholderImage:tmpImg];
//ios8layout會延遲到渲染的時候,這個地方需要算坐標,所以需要手動強迫layout
[buttonsetNeedsLayout];
[button layoutIfNeeded];
NSIntegertitleWidth = CGRectGetWidth(button.titleLabel.frame);
4、某些系統控件的動畫會有延遲現象:
例子:
[UIViewbeginAnimations:nil context:nil];
[UIViewsetAnimationDuration:duration];
[progressBar setAlpha:alpha];
progressBar.frame = frame;
[UIView commitAnimations];
以上代碼是閱讀器進度條變化的動畫,在IOS7工作良好,在Ios8中卻會出現進度后退的奇怪現象。
5、UITableViewCell有1個默許size(320,44):
在- (id)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier方法中不要依賴于self.frame或self.contentView.frame,由于這個時候的size都是默許值size(320,44).
有兩種解決方法,
1>>重寫setFrame方法,
-(void)setFrame:(CGRect)frame
{
frame.size.width=VIEW_WIDTH;//VIEW_WIDTH這里是屏幕豎屏時的寬
[supersetFrame:frame];
}
2>>在- (id)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier方法中把寬寫死,不要寫成根據self.frame.width算出來的值,例如需要與屏幕等寬,用[UIScreen mainScreen].bounds.width,
2、Iphone6&6p適配遇到的問題
1、如何管理圖片,對6p圖片加上@3x后綴,系統便可自動為我們加載所需要的圖片,但是對6的話,由于6和4/4s/5/5s的倍率是1樣的,如果想針對6單獨做圖片的話,系統就沒法知道我們要加載的圖片是哪個了。這個時候有兩種做法:
1》》做1個人為約定,例如針對6的圖片名字都加上“_6”后綴,然后針對UIImage做個擴大:
例如:
+(instancetype)imageNamedForDevices:(NSString*)imageName
{
NSRangeatPos=[imageName rangeOfString:@"@"];
if(atPos.location!=NSNotFound) {
imageName=[imageName substringToIndex:atPos.location];
}
UIImage* defaultImage=[UIImage imageNamed:imageName];
UIImage* iphone6Image=defaultImage;
if(![imageName hasSuffix:@"_6"])
{
iphone6Image=[UIImage imageNamed:[imageName stringByAppendingString:@"_6"]];
if(!iphone6Image) {
iphone6Image=defaultImage;
}
}
returnVALUE_FOR_UNIVERSE_DEVICE(defaultImage, iphone6Image, defaultImage);
}
這樣的話,加入我們有個圖片有針對5s,6,6p有3個版本,分別命名為home_bar@2x.png, home_bar_6@2x.png, home_bar@3x.png,
我們在程序中只要調用[UIImage imageNamedForDevices:@”home_bar”];就能夠根據裝備獲得到對應版本的圖片。
2》》手動寫if else語句,根據裝備編寫不同的獲得圖片的代碼,不推薦這類做法。
2、如何針對特定裝備做定制化需求:
typedef enum
{
iPhoneDeviceTypeIPhone4,
iPhoneDeviceTypeIPhone4S=iPhoneDeviceTypeIPhone4,
iPhoneDeviceTypeIPhone5,
iPhoneDeviceTypeIPhone5S=iPhoneDeviceTypeIPhone5,
iPhoneDeviceTypeIPhone6,
iPhoneDeviceTypeIPhone6P
}iPhoneDeviceType;
iPhoneDeviceType global_deviceType;
#define IS_IPHONE_5OR_ABOVE (global_deviceType>=iPhoneDeviceTypeIPhone5S)
#define IS_IPHONE_6P (global_deviceType==iPhoneDeviceTypeIPhone6P)
#define IS_IPHONE_6OR_ABOVE (global_deviceType>=iPhoneDeviceTypeIPhone6)
#define IS_IPHONE_6 (global_deviceType==iPhoneDeviceTypeIPhone6)
#define VALUE_FOR_UNIVERSE_DEVICE(a,b,c) ((IS_IPHONE_6P)?(a):((IS_IPHONE_6)?(b):(c)))
//獲得裝備尺寸信息
+(void)getDeviceType
{
iPhoneDeviceType type;
CGRectbounds=[[UIScreen mainScreen] bounds];
CGFloatheight=bounds.size.height;
CGFloatscale=[UIScreen mainScreen].scale;
if(height<568) {
type=iPhoneDeviceTypeIPhone4S;
}
elseif(height<667)
{
type=iPhoneDeviceTypeIPhone5S;
}
elseif(scale<2.9)
{
type=iPhoneDeviceTypeIPhone6;
}
else
{
type=iPhoneDeviceTypeIPhone6P;
}
global_deviceType=type;
}
VALUE_FOR_UNIVERSE_DEVICE這個宏能夠讓我們對裝備的判斷集中到1點,大大簡化了代碼,a,b,c可以是任意類型,只要保證a,b,c是同1類型便可,a是為6P定制的值,b是為6定制的值,c是為6以下設別定制的值。
3、觸及到橫豎屏切換的VC,代碼凡是用到UIScreen、UIWindow定位的地方都需要改寫,把width換成MIN(width,height),把height換成MAX(height,width).
4、坐標依賴法則:1>>ViewController中的所有坐標應當依賴于self.view.bounds;2>>UIView中的所有坐標應當依賴于self.bounds;3>>其他情況的坐標應當依賴于屏幕尺寸;1、2屬于相對坐標,3屬于絕對坐標。我們應當優先斟酌相對坐標,盡可能少用絕對坐標。
5、整體適配原則:1>>首先斟酌橫向充滿,縱向根據需要增加高度;
2>>海報、大圖片、小窗播放器根絕橫向放大系數進行等比放大;
3>>海報的內邊距保持不變,海報與文字的距離保持不變,文字字體、文字上下之間的間距根據視覺需要做微調;
4>>對那些沒有專門提供iPhone6&6p切圖的圖片,為了保證圖片不虛,需要把frame寫成與圖片尺寸1樣大。
6、關于適配解決方案的討論:
有3種方案:
1>>xib+autolayout;
這類方案是最為理想的適配方案,VC的坐標體系1目了然,只需要簡單計算便可適配新裝備。條件是源代碼用到了xib來寫VC.
2>>手寫VC+手寫autolayout;
個人認為這類方案是最為繁瑣的方案,首先需要大量的坐標計算,然后需要對VFL語法非常了解,解決束縛依賴缺失或沖突之類的問題,最后致使的結果是坐標計算繁瑣復雜,代碼改動非常大,代碼增加量很大,不容易于保護。
3>>手寫VC+手動適配;
這類方案也避免不了繁瑣的坐標計算,但是相對2方案來說,不需要去了解VFL語法,也不需要解決束縛依賴缺失或沖突之類的問題,代碼改動量相對較少。我們工程中目前采取的就是這類方案。