美文网首页
UIPopoverPresentationController(

UIPopoverPresentationController(

作者: 欧巴刚弄死他 | 来源:发表于2017-08-16 09:47 被阅读0次

    iOS8.0开始推荐使用UIPopoverPresentationController,用于替代UIPopoverController。
    UIPopoverPresentationController的使用方法类似于UIPopoverController,但是前者可以同时在iPhone和iPad上使用,这一点很不错。
    UIPopoverPresentationController并不是控制器类,而是继承自NSObject,不能直接被弹出。其创建的方法发生于控制器内部的代码:

    self.modalPresentationStyle = UIModalPresentationPopover;
    

    之后可以通过控制器的属性popoverPresentationController获取该对象。
    以下是控制器部分的代码,该控制器继承自UITableViewController:

    #import "HHContactsMenuController.h"
    
    static NSString *const HHContactsMenuControllerID = @"HHContactsMenuControllerID";
    
    @implementation HHContactsMenuController {
        NSArray<NSString *> *titles;
    }
    
    - (instancetype)init {
        self = [super init];
        if (self) {
            self.view.backgroundColor = ThemeColor();
            self.modalPresentationStyle = UIModalPresentationPopover;
            CGRect rect = CGRectMake(0, 0, 110, 96);
            self.preferredContentSize = rect.size;
            self.tableView.frame = rect;
            self.tableView.backgroundColor = ThemeColor();
            [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:HHContactsMenuControllerID];
            self.tableView.rowHeight = 32;
            titles = @[@"创建群聊", @"管理家庭圈", @"管理好友"];
        }
        return self;
    }
    
    #pragma mark - Table view data source
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 3;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HHContactsMenuControllerID forIndexPath:indexPath];
        
        cell.textLabel.text = titles[indexPath.row];
        cell.textLabel.textColor = UIWhiteColor();
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        cell.backgroundColor = UIClearColor();
        CGFloat xx = 15;
        if (indexPath.row == 2) {
            cell.separatorInset = UIEdgeInsetsMake(0, xx, 0, tableView.frame.size.width - xx);
        } else {
            cell.separatorInset = UIEdgeInsetsMake(0, xx, 0, 0);
        }
        
        return cell;
    }
    
    @end
    

    其中UIPopoverPresentationController的代理adaptivePresentationStyleForPresentationController:一定要实现,并且返回值必须是UIModalPresentationNone!以下是调用部分的代码:

    - (void)rightBarButtonItemTapped:(id)sender {
        HHContactsMenuController *cmc = HHContactsMenuController.new;
        UIPopoverPresentationController *pop = cmc.popoverPresentationController;
        pop.barButtonItem = self.navigationItem.rightBarButtonItem;
        pop.permittedArrowDirections = UIPopoverArrowDirectionUp;
        pop.backgroundColor = ThemeColor();
        pop.delegate = self;
        [self presentViewController:cmc animated:YES completion:^{
            
        }];
    }
    #pragma mark- UIPopoverPresentationControllerDelegate
    -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
        return UIModalPresentationNone;
    }
    

    iPad上的效果没有试过....😝

    相关文章

      网友评论

          本文标题:UIPopoverPresentationController(

          本文链接:https://www.haomeiwen.com/subject/plnbrxtx.html