由于项目中用到一个小功能所以写出来和大家分享!
直接复制粘贴就可以了!!!!! 就不往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
网友评论