UIPopoverController只能跑在iPad上
- (IBAction)click:(id)sender;
@property (nonatomic,strong) UIPopoverController *popOver;
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(barClick)];
TQTableViewController *tableViewVC = [[TQTableViewController alloc] initWithStyle:UITableViewStylePlain];
self.popOver = [[UIPopoverController alloc] initWithContentViewController:tableViewVC];//改变弹出框的大小
self.popOver.popoverContentSize = CGSizeMake(200, 200);
}(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//vc变成弹出框
-(void)barClick
{
warning 8.0之前
//在导航条按钮附近弹出
//[self.popOver presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
warning 8.0之后
tableViewVC = [[TQTableViewController alloc] init];
tableViewVC.block = ^(UIColor *color)
{
self.view.backgroundColor = color;
[tableViewVC dismissViewControllerAnimated:YES completion:nil];
};
//改变弹出视图的样式为Popover
tableViewVC.modalPresentationStyle = UIModalPresentationPopover;
//弹出框的大小
tableViewVC.preferredContentSize = CGSizeMake(200, 200);
//设置显示的按钮
tableViewVC.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem;
//设置弹出框在哪个view中显示
tableViewVC.popoverPresentationController.sourceView = self.view;
//设置箭头的方向
tableViewVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
//弹出当前VC
[self presentViewController:tableViewVC animated:YES completion:nil];
}
(IBAction)click:(UIButton *)sender {
warning 8.0之前//在指定的frame附近弹出
// [self.popOver presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
warning 8.0之后
TQTableViewController *tableViewVC = [[TQTableViewController alloc] init];
tableViewVC.modalPresentationStyle = UIModalPresentationPopover;
tableViewVC.preferredContentSize = CGSizeMake(200, 200);
tableViewVC.popoverPresentationController.sourceRect = sender.frame;
tableViewVC.popoverPresentationController.sourceView = self.view;
tableViewVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:tableViewVC animated:YES completion:nil];
}