Henning
Henning

Reputation: 491

iOS - UIViewController as Popup with dynamic height

I have a Popup-View which contains two labels, a tableview and button. I created a ViewController as in Display UIViewController as Popup in iPhone described.

My special requirement is now, that the tableview is not necessary in all cases, so I tried to hide it and expected a reduced height of the Popup-View. But I always get the same height. I also tried to use a UIStackView, but the height of the view wasn't changed in case of hiding the tableview.

I also need to have the view in center of the display in both cases of height.

enter image description here

@interface AuthorizationMessageViewController ()

@property (weak, nonatomic) IBOutlet UIView *messageView;
@property (weak, nonatomic) IBOutlet UILabel *titelLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailsLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *okButton;
- (IBAction)okButtonTouchUp:(id)sender;
@end

@implementation AuthorizationMessageViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.messageView.layer.cornerRadius = 5;
    self.messageView.layer.masksToBounds = YES;
    self.messageView.backgroundColor = COLOR_BACKGROUND_WHITE;
    self.messageView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.backgroundColor = COLOR_BACKGROUND_WHITE;

    self.titelLabel.text = WHLocalizedString(@"EventHeaderAuthorization", nil);

    [self setupView];
}

- (void)setupView
{
    NSString *ns_messageText;

    ns_messageText = @"test";

    if (YES)
    {
        ns_messageText = @"Hide"
        [self.tableView setHidden:YES];
    }
    else
    {
        ns_messageText = @"No Hide"
        [self.tableView setHidden:NO];
    }

    self.detailsLabel.text = ns_messageText;
    self.detailsLabel.textColor = COLOR_TEXT_GREY_KEY;
    self.detailsLabel.numberOfLines = 0;
    [self.detailsLabel sizeToFit];
}

#pragma mark - Table view data source

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

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 21;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"testCell"];

    UILabel *weekDayLabel = (UILabel *)[cell viewWithTag:10];
    weekDayLabel.text = @"weekday";
    weekDayLabel.textColor = COLOR_TEXT_GREY_KEY;
    weekDayLabel.font = FONT_LIGHT_SIZE_15;

    UILabel *testLabel = (UILabel *)[cell viewWithTag:11];
    testLabel = @"testLabel"
    testLabel = COLOR_TEXT_GREY_KEY;
    testLabel = FONT_LIGHT_SIZE_15;

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;
}

I hope that someone has a solution or an idea for that.

Upvotes: 0

Views: 1081

Answers (3)

Shruti
Shruti

Reputation: 1849

Try to Adjust the Height of your view using the NSLayoutConstraint. Create an outlet for the same and manage height programatically. For example:

 myConstraintOutlet.constant = 10
 myTableView.layoutIfNeeded()

Here is the link for further assistant.

Regarding your second query to have the pop up always in centre you can either do it using Autolayout or programatically define the centre of your pop up.

 myView.center = CGPoint(x: superView.center.x, y: superView.center.y)

Hope that helps.

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100533

Imagine the following UI

enter image description here

a centered UIView that contains a UIButton and UITableView , you need to hook height constraint of the table and do this inside the popup if you want to hide it

@IBOutlet weak var heightTblCon:NSLayoutConstraint!

//

self.heightTblCon.constant = show ? 300 : 0
self.view.layoutIfNeeded()

BTW i changed color of background view for clarification purposes that should be transparent for modals

Upvotes: 1

Farid Rahmani
Farid Rahmani

Reputation: 113

Try calling sizeToFit() on Popup-View after hiding the tableView:

popupView.sizeToFit()

Upvotes: 0

Related Questions