简体   繁体   中英

How to set the UITableView position and size in UIAlertController with iOS8

I want to build a UITableView in UIAlertController with iOS8.

I want the effect like below, this is iOS8 original component ,popup the alertview and inside the UITableView.

在此处输入图片说明

I try to build the UITableVIew in the UIAlertController, but I don't know how the set the size and position like the photo.

Have any one know how the adjust the UITableView position and size in the UIAlertController?

my code below:

 @interface ViewController ()
 @property (nonatomic,strong) UITableView *tableView;
 @property(strong,nonatomic) NSMutableArray *titleArray;
 @end

 @implementation ViewController

 - (void)viewDidLoad {
     [super viewDidLoad];
  self.titleArray = [[UIFont familyNames] mutableCopy];
     for(int i = 0; i < 100; i++) {
         [self.titleArray addObjectsFromArray:[UIFont familyNames]];
     }

      dispatch_async(dispatch_get_main_queue(), ^{
          [self showUIAlertTable];
      });

 }

 -(void) showUIAlertTable
 {
     UIAlertController *alert = [UIAlertController      alertControllerWithTitle:nil
                                                                    message:@"select network\n\n\n"
                                                             preferredStyle:UIAlertControllerStyleAlert];


     self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds      style:UITableViewStylePlain];
     self.tableView.dataSource = self;
     self.tableView.delegate = self;

     [alert.view addSubview:self.tableView];
     [self presentViewController:alert animated:NO completion:nil];

 }


 #pragma mark - UITableViewDataSource Methods

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
     return 1;
 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:     (NSInteger)section
 {
     return [self.titleArray count];
 }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     static NSString *CellIdentifier = @"Cell";

     UITableViewCell *cell = [tableView      dequeueReusableCellWithIdentifier:CellIdentifier];

     if (cell == nil) {

         cell = [[UITableViewCell alloc]      initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;

     }
     cell.textLabel.text = [self.titleArray objectAtIndex:indexPath.row];
     [cell setNeedsUpdateConstraints];

     return cell;
 }

The code effect run effect below:

在此处输入图片说明

It is not my expect effect.

Have anyone can teach me how to fix?

Thank you very much.

(I had use autolayout)

UIAlertController seems to expose a very limited API, intended for specific cases of some 'actions', 'textfields', and a cancel button...

If you want to do any other layout, just build a specific UIViewController , and customize its presentation. ( Here's an example )

All of this is only compatible with iOS 8 and above, if you need to support iOS 7, other hacks are needed...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM