UIDatePicker
在 iOS 14 開始,UIDatePicker
默認樣式為:
而在 iOS14 之前的樣式是
同樣的代碼怀跛,顯示樣式不一樣
UIDatePicker *datePicer = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 200, self.width, 100)];
datePicer.backgroundColor = [UIColor whiteColor];
[self addSubview:datePicer];
雖然有設(shè)置 UIDatePicker
的 frame
,但是在 iOS 14 上完全沒有效果,要想在 iOS 14 上顯示跟之前一樣逊拍,還要再設(shè)置 preferredDatePickerStyle
這個屬性為 UIDatePickerStyleWheels
.
/// Request a style for the date picker. If the style changed, then the date picker may need to be resized and will generate a layout pass to display correctly.
@property (nonatomic, readwrite, assign) UIDatePickerStyle preferredDatePickerStyle API_AVAILABLE(ios(13.4)) API_UNAVAILABLE(tvos, watchos);
對于這個屬性幻林,是 UIDatePicker
的樣式抡柿,如果樣式發(fā)生了更改冲甘,則可能需要調(diào)整 UIDatePicker
的大小并生成布局展示出來
所以如果只是單單設(shè)置了這個屬性還不行捷雕,還需要再重新設(shè)置 Frame
和 backgroundColor
UIDatePicker *datePicer = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 200, self.width, 100)];
if (@available(iOS 13.4, *)) {
datePicer.preferredDatePickerStyle = UIDatePickerStyleWheels; // 只設(shè)置了 preferredDatePickerStyle 屬性
}
datePicer.backgroundColor = [UIColor whiteColor];
[self addSubview:datePicer];
CGRect frame = CGRectMake(0, 200, self.width, 100);
UIDatePicker *datePicer = [[UIDatePicker alloc] initWithFrame:CGRectZero];
if (@available(iOS 13.4, *)) {
datePicer.preferredDatePickerStyle = UIDatePickerStyleWheels; // 只設(shè)置了 preferredDatePickerStyle 屬性
}
datePicer.backgroundColor = [UIColor whiteColor];
datePicer.frame = frame;
[self addSubview:datePicer];
UITableViewCell
在 iOS 14 環(huán)境下瘫析,UITableViewCell
的結(jié)構(gòu)如下:
而在 iOS 14 之前,UITableViewCell
的結(jié)構(gòu)如下:
對比可以發(fā)現(xiàn),iOS14 多了一個 _UISystemBackgroundView
和一個子視圖
如果我們在cell 中創(chuàng)建新的 UI 控件贬循,然后直接添加到 cell
中,所以在 iOS14 下,如果直接講 UI 空間添加到 cell 上面,默認會放在 contentView
下面,如果有一些交互事件,這時候是無法響應(yīng)的,因為被 contentView
給擋住了,所以需要添加到 contentView
上面.
UIPageControl
在之前,如果想要修改 UIPageControl
默認圖片和選中圖片,需要按照如下方式修改:
[_pageControl setValue:_pageIndicatorImage forKeyPath:@"pageImage"];
[_pageControl setValue:_currentPageIndicatorImage forKeyPath:@"currentPageImage"];
但是在 iOS14 開始,這樣修改直接一個異常,提示調(diào)用過時的私有方法:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Call to obsolete private method -[UIPageControl _setPageImage:]'
UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 100, self.view.width, 30)];
pageControl.backgroundColor = [UIColor orangeColor];
pageControl.numberOfPages = 6;
if (@available(iOS 14.0, *)) {
pageControl.backgroundStyle = UIPageControlBackgroundStyleMinimal;
pageControl.allowsContinuousInteraction = false;
pageControl.preferredIndicatorImage = [UIImage imageNamed:@"page_currentImage"];
// 目前發(fā)現(xiàn)只能通過這樣的方式去設(shè)置當(dāng)前選中的圖片顏色
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
[pageControl setIndicatorImage:[UIImage imageNamed:@"live"] forPage:2];
} else {
[pageControl setValue:[UIImage imageNamed:@"page_image"] forKeyPath:@"pageImage"];
[pageControl setValue:[UIImage imageNamed:@"page_currentImage"] forKeyPath:@"currentPageImage"];
}
[self.view addSubview:pageControl];
運行不同環(huán)境
iOS 14 之前環(huán)境
iOS 14 之后環(huán)境
CALayer 的 mask
公司所在的項目中,聊天界面利用CALayer
的 mask
方式將背景弄成一個氣泡的樣式,在 iOS 14 之前是好的,但是在 iOS 14 上就顯示不出來了.具體的方式是將一個氣泡圖片,用一個 UIImageView
加載出來,然后將這個氣泡的 ImageView
的 layer
作為一個遮罩,放在圖片消息上面去.
代碼類似下面的:
UIImageView *imageView = [UIImageView new];
imageView.image = [UIImage imageNamed:@"calendar"];
imageView.size = CGSizeMake(200, 200);
imageView.center = self.center;
[self addSubview:imageView];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 80)];
imgView.image = [UIImage imageNamed:@"green_pop"];
imageView.layer.mask = imgView.layer;
以上代碼運行結(jié)果,在不同的環(huán)境下,顯示出來的效果不一樣
iOS14 顯示不出來,但是在 iOS12.4 卻能顯示出來
后面在添加到 imageView.layer.mask
之前,將 imgView
添加到某個視圖上去,發(fā)現(xiàn)在 iOS 14
上又能顯示出來,所以想是不是在 iOS14
上的渲染邏輯發(fā)生了改變
UIImageView *imageView = [UIImageView new];
imageView.image = [UIImage imageNamed:@"calendar"];
imageView.size = CGSizeMake(200, 200);
imageView.center = self.center;
[self addSubview:imageView];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 80)];
imgView.image = [UIImage imageNamed:@"green_pop"];
[imageView addSubview:imgView];
imageView.layer.mask = imgView.layer;
其實可以直接使用 layer
的 content
進行設(shè)置圖片
UIImageView *imageView = [UIImageView new];
imageView.image = [UIImage imageNamed:@"calendar"];
imageView.size = CGSizeMake(200, 200);
imageView.center = self.center;
[self addSubview:imageView];
CALayer *maskLayer = [[CALayer alloc] init];
maskLayer.frame = CGRectMake(0, 0, 180, 90);
maskLayer.contents = (__bridge id)[UIImage imageNamed:@"green_pop"].CGImage;
imageView.layer.mask = maskLayer;
這樣也能顯示出來
AssetsLibrary
AssetsLibrary
在 iOS9 已經(jīng)開始被棄用了咸包,但是一些老的項目還在使用這個庫進行相冊訪問,經(jīng)過測試杖虾,同樣的代碼烂瘫,在 iOS 14 下拿到相冊中的圖片之后,獲取圖片的大小已經(jīng)獲取不到了
iOS14 之前獲取圖片大小情況
iOS 14 獲取圖片大小情況
PHPickerViewController 的使用
在 iOS14 增加了一個 PHPickerViewController
對相冊的訪問,替代 UIImagePickerController
.
我們可以在 UIImagePickerController.h
文件中看到,蘋果推薦使用 PHPickerViewController
去訪問相冊
首先,創(chuàng)建一個 PHPickerConfiguration
進行一些配置
PHPickerConfiguration *configuration = [PHPickerConfiguration new];
configuration.filter = [PHPickerFilter imagesFilter]; // 設(shè)置所選的類型,這里設(shè)置是圖片,默認是 nil,設(shè)置成 nil 則代表所有的類型都顯示出來(包括 視頻/LivePhoto )
configuration.selectionLimit = 10; // 設(shè)置可選擇的最大數(shù),默認為 1
創(chuàng)建 PHPickerViewController
, 并進行跳轉(zhuǎn)
PHPickerViewController *picker = [[PHPickerViewController alloc] initWithConfiguration:configuration];
picker.delegate = self;
picker.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:picker animated:YES completion:nil];
實現(xiàn)代理方法 picker:didFinishPicking:results
- (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPickerResult *> *)results API_AVAILABLE(ios(14)){
[picker dismissViewControllerAnimated:YES completion:nil];
if (!results || !results.count) return;
// 遍歷獲取到的結(jié)果
for (PHPickerResult *result in results) {
NSItemProvider *itemProvider = result.itemProvider;
if ([itemProvider canLoadObjectOfClass:UIImage.class]) {
[itemProvider loadObjectOfClass:UIImage.class
completionHandler:^(__kindof id<NSItemProviderReading> _Nullable object, NSError * _Nullable error) {
// 取出圖片
if (!error && object && [object isKindOfClass:UIImage.class]) {
NSLog(@"%@", object);
}
}];
}
}
}