iOS - UIViewController 作为具有动态高度的弹出窗口
iOS - UIViewController as Popup with dynamic height
我有一个弹出视图,其中包含两个标签、一个表格视图和一个按钮。我按照 Display UIViewController as Popup in iPhone 中的描述创建了一个 ViewController。
我现在的特殊要求是,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 后尝试在 Popup-View 上调用 sizeToFit()
:
popupView.sizeToFit()
想象一下UI
a centered UIView
that contains a UIButton
and UITableView
,你需要挂钩 table 的高度约束,如果你想在弹出窗口中执行此操作隐藏它
@IBOutlet weak var heightTblCon:NSLayoutConstraint!
//
self.heightTblCon.constant = show ? 300 : 0
self.view.layoutIfNeeded()
顺便说一句,为了清楚起见,我更改了背景视图的颜色,这对于模式应该是透明的
尝试使用 NSLayoutConstraint
调整视图的高度。为它创建一个出口并以编程方式管理高度。例如:
myConstraintOutlet.constant = 10
myTableView.layoutIfNeeded()
是 link 进一步的助手。
关于让弹出窗口始终居中的第二个查询,您可以使用 Autolayout
或以编程方式定义弹出窗口的中心。
myView.center = CGPoint(x: superView.center.x, y: superView.center.y)
希望对您有所帮助。
我有一个弹出视图,其中包含两个标签、一个表格视图和一个按钮。我按照 Display UIViewController as Popup in iPhone 中的描述创建了一个 ViewController。
我现在的特殊要求是,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 后尝试在 Popup-View 上调用 sizeToFit()
:
popupView.sizeToFit()
想象一下UI
a centered UIView
that contains a UIButton
and UITableView
,你需要挂钩 table 的高度约束,如果你想在弹出窗口中执行此操作隐藏它
@IBOutlet weak var heightTblCon:NSLayoutConstraint!
//
self.heightTblCon.constant = show ? 300 : 0
self.view.layoutIfNeeded()
顺便说一句,为了清楚起见,我更改了背景视图的颜色,这对于模式应该是透明的
尝试使用 NSLayoutConstraint
调整视图的高度。为它创建一个出口并以编程方式管理高度。例如:
myConstraintOutlet.constant = 10
myTableView.layoutIfNeeded()
关于让弹出窗口始终居中的第二个查询,您可以使用 Autolayout
或以编程方式定义弹出窗口的中心。
myView.center = CGPoint(x: superView.center.x, y: superView.center.y)
希望对您有所帮助。