iOS15 UISheetPresentationController使用

代碼


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.selectImageView];
    [self.view addSubview:self.imagePickerButton];
    [self.view addSubview:self.customVCButton];
}

- (void)openImagePicker:(UIButton *)button {
    [self openViewController:self.imagePicker button:button];
    
}

- (void)openCustomViewController:(UIButton *)button {
    [self openViewController:self.tableViewVC button:button];
}

- (void)openViewController:(UIViewController *)vc button:(UIButton *)button {
    if (!_isOpen) {
        
        UIPopoverPresentationController *popover = vc.popoverPresentationController;
        UISheetPresentationController *sheet = popover.adaptiveSheetPresentationController;
        sheet.detents = @[UISheetPresentationControllerDetent.mediumDetent,
                          UISheetPresentationControllerDetent.largeDetent ];
        sheet.smallestUndimmedDetentIdentifier = UISheetPresentationControllerDetentIdentifierMedium;
        sheet.prefersScrollingExpandsWhenScrolledToEdge = false;
        sheet.prefersEdgeAttachedInCompactHeight = true;
        sheet.widthFollowsPreferredContentSizeWhenEdgeAttached = true;
        
        [self presentViewController:vc animated:YES completion:^{
            [button setTitle:@"close" forState:UIControlStateNormal];
        }];
    } else {
        [vc dismissViewControllerAnimated:NO completion:^{
            [button setTitle:@"open" forState:UIControlStateNormal];
        }];
    }
    
    _isOpen = !_isOpen;
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [_customVCButton setTitle:@"open" forState:UIControlStateNormal];
    [_imagePickerButton setTitle:@"open" forState:UIControlStateNormal];
    _isOpen = false;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController {
    [_customVCButton setTitle:@"open" forState:UIControlStateNormal];
    [_imagePickerButton setTitle:@"open" forState:UIControlStateNormal];
    _isOpen = false;
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self.selectImageView setImage:image];
}

- (UIImagePickerController *)imagePicker {
    if(!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc] init];
        _imagePicker.delegate = self;
        _imagePicker.modalPresentationStyle = UIModalPresentationPopover;

    }
    return _imagePicker;
}

- (TableViewViewController *)tableViewVC {
    if (!_tableViewVC) {
        _tableViewVC = [[TableViewViewController alloc] init];
        _tableViewVC.modalPresentationStyle = UIModalPresentationPopover;
    }
    return _tableViewVC;
}

- (UIButton *)imagePickerButton {
    if (!_imagePickerButton) {
        _imagePickerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 84, self.view.frame.size.width/2, 40)];
        _imagePickerButton.backgroundColor = [UIColor clearColor];
        _imagePickerButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_imagePickerButton addTarget:self action:@selector(openImagePicker:) forControlEvents:UIControlEventTouchUpInside];
        [_imagePickerButton setTitle:_isOpen ? @"lose" : @"open" forState:UIControlStateNormal];
        [_imagePickerButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _imagePickerButton;
}

- (UIButton *)customVCButton {
    if (!_customVCButton) {
        
        _customVCButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, 84, self.view.frame.size.width/2, 40)];
        _customVCButton.backgroundColor = [UIColor clearColor];
        _customVCButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_customVCButton addTarget:self action:@selector(openCustomViewController:) forControlEvents:UIControlEventTouchUpInside];
        [_customVCButton setTitle:_isOpen ? @"close" : @"open" forState:UIControlStateNormal];
        [_customVCButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _customVCButton;
}

- (UIImageView *)selectImageView {
    if(!_selectImageView) {
        double height = MIN(self.view.frame.size.height /2, self.view.frame.size.width)  - 100 - 40;
        _selectImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 84 + 40, height, height)];
        _selectImageView.layer.cornerRadius = 20;
        _selectImageView.layer.masksToBounds = YES;
    }
    return _selectImageView;
}

效果

效果圖.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末辅辩,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子络拌,更是在濱河造成了極大的恐慌珠月,老刑警劉巖辅搬,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件立砸,死亡現(xiàn)場(chǎng)離奇詭異洞坑,居然都是意外死亡缠局,警方通過(guò)查閱死者的電腦和手機(jī)呀页,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門妈拌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人蓬蝶,你說(shuō)我怎么就攤上這事尘分。” “怎么了疾党?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵音诫,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我雪位,道長(zhǎng)竭钝,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任雹洗,我火速辦了婚禮香罐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘时肿。我一直安慰自己庇茫,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布螃成。 她就那樣靜靜地躺著旦签,像睡著了一般查坪。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上宁炫,一...
    開(kāi)封第一講書(shū)人閱讀 51,182評(píng)論 1 299
  • 那天偿曙,我揣著相機(jī)與錄音,去河邊找鬼羔巢。 笑死望忆,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的竿秆。 我是一名探鬼主播启摄,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼幽钢!你這毒婦竟也來(lái)了歉备?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤匪燕,失蹤者是張志新(化名)和其女友劉穎威创,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體谎懦,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年溃斋,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了界拦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡梗劫,死狀恐怖享甸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情梳侨,我是刑警寧澤蛉威,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站走哺,受9級(jí)特大地震影響蚯嫌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜丙躏,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一择示、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧晒旅,春花似錦栅盲、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)扒寄。三九已至,卻和暖如春拟烫,著一層夾襖步出監(jiān)牢的瞬間该编,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工构灸, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留上渴,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓喜颁,卻偏偏與公主長(zhǎng)得像稠氮,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子半开,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容