【问题标题】:iOS - UIViewController as Popup with dynamic heightiOS - UIViewController 作为具有动态高度的弹出窗口
【发布时间】:2019-02-21 04:20:33
【问题描述】:

我有一个包含两个标签、一个表格视图和按钮的弹出视图。我创建了一个 ViewController,如Display UIViewController as Popup in iPhone 所述。

我现在的特殊要求是,并不是所有情况下都需要 tableview,所以我试图隐藏它并期望降低 Popup-View 的高度。但我总是得到相同的高度。我也尝试使用 UIStackView,但是在隐藏 tableview 的情况下视图的高度没有改变。

在两种高度情况下,我还需要将视图置于显示器的中心。

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;
}

我希望有人对此有解决方案或想法。

【问题讨论】:

  • 提供试用码。
  • 试试这个tableView.isHidden = truetableView.frame = CGRect.zero

标签: ios objective-c uiviewcontroller


【解决方案1】:

想象一下下面的 UI

一个居中的 UIView 包含一个 UIButtonUITableView ,如果你想隐藏它,你需要挂钩表格的高度约束并在弹出窗口中执行此操作

@IBOutlet weak var heightTblCon:NSLayoutConstraint!

//

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

顺便说一句,我改变了背景视图的颜色,以澄清对于模态应该是透明的

【讨论】:

  • 该解决方案对我帮助很大。谢谢!
【解决方案2】:

隐藏tableView后尝试在Popup-View上调用sizeToFit()

popupView.sizeToFit()

【讨论】:

    【解决方案3】:

    尝试使用NSLayoutConstraint 调整视图的高度。为相同的插座创建一个插座并以编程方式管理高度。例如:

     myConstraintOutlet.constant = 10
     myTableView.layoutIfNeeded()
    

    Here 是进一步助手的链接。

    关于您的第二个查询始终使弹出窗口居中,您可以使用Autolayout 或以编程方式定义弹出窗口的中心。

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

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      • 2011-10-20
      • 2011-10-01
      • 2019-12-12
      相关资源
      最近更新 更多