简体   繁体   English

如何使用iOS8在UIAlertController中设置UITableView的位置和大小

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

I want to build a UITableView in UIAlertController with iOS8. 我想在iOS8的UIAlertController中构建一个UITableView。

I want the effect like below, this is iOS8 original component ,popup the alertview and inside the UITableView. 我想要如下效果,这是iOS8的原始组件,弹出alertview并在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. 我尝试在UIAlertController中构建UITableVIew,但是我不知道如何设置照片的大小和位置。

Have any one know how the adjust the UITableView position and size in the UIAlertController? 有谁知道如何在UIAlertController中调整UITableView的位置和大小?

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... UIAlertController似乎公开了一个非常有限的API,用于某些“动作”,“文本字段”和取消按钮的特定情况。

If you want to do any other layout, just build a specific UIViewController , and customize its presentation. 如果要进行任何其他布局,只需构建特定的UIViewController并自定义其表示即可。 ( 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... 所有这些仅与iOS 8及更高版本兼容,如果您需要支持iOS 7,则需要其他技巧...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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