// // popViewController.m // Haishenghai-master // // Created by GG on 2019/1/3. // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved. // #import "popViewController.h" @interface popViewController () @property (nonatomic,strong)UITableView *tableView; @end @implementation popViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableView = [[UITableView alloc] initWithFrame:self.view.frame]; [self.view addSubview:self.tableView]; self.tableView.dataSource = self; self.tableView.delegate = self; self.tableView.scrollEnabled = NO; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 2; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellid = @"cellID"; UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellid]; if (!cell) { cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid]; } cell.imageView.image = [UIImage imageNamed:@"hsh_home_add"]; NSArray *title = @[@"添加设备",@"添加场所"]; cell.textLabel.text = title[indexPath.row]; return cell; } //-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ // return 44; //} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //创建消息,在点击相应的颜色时发送,在ViewController中接受消息并做出相应的处理 [[NSNotificationCenter defaultCenter] postNotificationName:@"click" object:indexPath]; } //重写preferredContentSize(iOS7之后)来返回最合适的大小,如果不重写,会返回一整个tableview尽管下面一部分cell是没有内容的,重写后只会返回有内容的部分,我这里还修改了宽,让它窄一点。可以尝试注释这一部分的代码来看效果,通过修改返回的size得到你期望的popover的大小。 - (CGSize)preferredContentSize { if (self.presentingViewController && self.tableView != nil) { CGSize tempSize = self.presentingViewController.view.bounds.size; tempSize.width = 130; CGSize size = [self.tableView sizeThatFits:tempSize]; //sizeThatFits返回的是最合适的尺寸,但不会改变控件的大小 return size; }else { return [super preferredContentSize]; } } - (void)setPreferredContentSize:(CGSize)preferredContentSize{ super.preferredContentSize = preferredContentSize; } - (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