[iOS功能]- 弹出小圆角 UIPopoverPresentationController

由于项目中用到一个小功能所以写出来和大家分享!

直接复制粘贴就可以了!!!!!  就不往github上传了!!!!!!

ViewController.m

//

//  ViewController.m

//  pop圆角

//

//  Created by 嘟嘟 on 2017/8/28.

//  Copyright © 2017年 MC. All rights reserved.

//

#define SCR_W [UIScreen mainScreen].bounds.size.width

#define SCR_H [UIScreen mainScreen].bounds.size.height

#import "ViewController.h"

#import "PopTableviewVC.h"

@interface ViewController ()

<

UIPopoverPresentationControllerDelegate,

CallBackWithActionIdSelected

>

{

UIButton * btn;

UIButton * btn1;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 80, 100, 30)];

btn.backgroundColor = [UIColor redColor];

[btn addTarget:self action:@selector(clockBtn) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

- (void)clockBtn

{

PopTableviewVC * popInTableVC = [[PopTableviewVC alloc]init];

//    popInTableVC.actionIdList = nil; //传过去的数组

popInTableVC.callBackDelegate = self;

popInTableVC.preferredContentSize = CGSizeMake(100, 120);        // 设置大小

popInTableVC.modalPresentationStyle = UIModalPresentationPopover;// 设置 Sytle

popInTableVC.popoverPresentationController.sourceView = btn;    // 需要通过 sourceView 来判断位置的

// 指定箭头所指区域的矩形框范围(位置和尺寸),以sourceView的左上角为坐标原点

// 这个可以 通过 Point 或  Size 调试位置

popInTableVC.popoverPresentationController.sourceRect = btn.bounds;

popInTableVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;// 箭头方向

popInTableVC.popoverPresentationController.delegate = self;// 设置代理

[self presentViewController:popInTableVC animated:YES completion:nil];

}

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller

{

return UIModalPresentationNone; //不适配

}

//- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController

//{

//    return YES;  //点击蒙版popover消失, 默认YES

//}

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController

{

}

- (void)callbackWithActionId:(NSInteger)newActionId

{

NSLog(@"popSelect----%zi",newActionId);

[btn setTitle:[NSString stringWithFormat:@"%zi",newActionId] forState:UIControlStateNormal];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end


PopTableviewVC.h

////  PopTableviewVC.h//  pop圆角////  Created by 嘟嘟 on 2017/8/28.//  Copyright © 2017年 MC. All rights reserved.//#import@protocol CallBackWithActionIdSelected- (void)callbackWithActionId:(NSInteger)actionId;@end@interface PopTableviewVC : UIViewController@property NSArray *actionIdList;@property (weak) idcallBackDelegate;


@end

PopTableviewVC.m

////  PopTableviewVC.m//  pop圆角////  Created by 嘟嘟 on 2017/8/28.//  Copyright © 2017年 MC. All rights reserved.//#import "PopTableviewVC.h"@interface PopTableviewVC ()@end

@implementation PopTableviewVC

@synthesize actionIdList;

@synthesize callBackDelegate;

- (void)viewDidLoad {

[super viewDidLoad];

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 150) style:UITableViewStylePlain];

tableView.dataSource = self;

tableView.delegate = self;

[self.view addSubview:tableView];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return 3;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellId"];

}

cell.textLabel.text = @"骑行";

cell.textLabel.font = [UIFont systemFontOfSize:14];

return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 30;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

//设置段头高度

return  0.01;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

//设置段尾高度

return 0.01;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

[self dismissViewControllerAnimated:YES completion:^{

if (callBackDelegate) {

[callBackDelegate callbackWithActionId:indexPath.row];

}

}];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • UIView+SGFrame.h #import @interface UIView (SGFrame) @pro...
    超越时空的思念_8a22阅读 1,843评论 1 1
  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,682评论 3 3
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,138评论 1 32
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一个文章 AppDelegate.h ...
    CC_iOS阅读 948评论 0 0
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,542评论 1 14