如何从UIAlertView中删除额外的上下间距?

时间:2014-12-03 12:36:18

标签: ios objective-c iphone

使用UIAlertView创建以下警报,显示顶部和顶部的额外空格底部如图1所示 我想删除这种间距,如图2所示 请提出任何解决方案。


我的警报代码:使用iOS 7.1

 - (id)initWithFrame:(CGRect)frame Cancel:(NSString *)CancelValue Set:(NSString *)SetValue Tag:(int)TagValue Array:(NSMutableArray *)ArrayValue
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];

        UIView *vwAlert = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 380)];
        [alert addSubview:vwAlert];

        UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, 280, 350) style:UITableViewStylePlain];
        table.dataSource = self;
        table.delegate = self;
        table.scrollEnabled = NO;
        table.tag = TagValue;
        [table setSeparatorInset:UIEdgeInsetsZero];
        [vwAlert addSubview:table];

        aryTitle = [[NSMutableArray alloc] initWithObjects:@"Sort Habits",@"Habit Records",@"Category", nil];
        vwAlert.frame = CGRectMake(0, 0, 320, 170);
        table.frame = CGRectMake(20, 0, 280, vwAlert.frame.size.height);

        btnCancel.hidden=YES;
        btnSet.hidden=YES;

        UIImageView *imgVw = [[UIImageView alloc] initWithFrame:CGRectMake(250, 2, 40, 40)];
        imgVw.image = [UIImage imageNamed:@"icon_filter_clear"];
        [vwAlert addSubview:imgVw];

        UIButton *btnClearAll = [UIButton buttonWithType:UIButtonTypeSystem];
        btnClearAll.frame = CGRectMake(235, 0, 60, 40);
        [btnClearAll addTarget:self action:@selector(funClearAll:) forControlEvents:UIControlEventTouchUpInside];
        [vwAlert addSubview:btnClearAll];

        [table reloadData];
        [alert setValue:vwAlert forKey:@"accessoryView"];
        [alert show];
    }
    return self;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (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.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    cell.textLabel.text = [aryRestore objectAtIndex:(indexPath.row)];
    cell.textLabel.font = [UIFont fontWithName:@"Avenir Next" size:16];
    return cell;
}


图片1
enter image description here

图片2
enter image description here

1 个答案:

答案 0 :(得分:0)

创建如下的自定义提醒视图。

<强> CustomAlertView.h

@interface CustomAlertView:UIAlertView {}

<强> CustomAlertView.m

// override
- (void)drawRect:(CGRect)rect
{
    for(UIView * sub in self.subviews)
    {
        [sub removeFromSuperview];
    }
    // this removeFromSuperview will clear all thing such as background view and buttons

    // do something
}

现在在您的视图控制器中导入此customalertview,如下所示。

#import "CustomAlertView.h"

然后而不是

UIAlertView *alert = [[UIAlertView alloc]

使用

CustomAlertView *alert = [[CustomAlertView alloc]

注意

使用UIAlertView进行操作是不对的。我建议使用像KGModal这样的popover。