简体   繁体   English

如何强制按钮停留在tableView单元格的底部?

[英]How to force button to stay at tableView cell's bottom?

I add a button as a subview to my tableviewcells. 我在tableviewcells中添加了一个按钮作为子视图。 Works fine. 工作正常。 But I can't seem to figure out, how to force it to always be at the bottom of the cell. 但我似乎无法弄明白,如何强迫它总是在细胞的底部。 (On top of the separator line if you will). (如果愿意,在分隔线的顶部)。 The cell height varies, so a fixed position is not smart. 单元高度不同,因此固定位置不智能。 But I have no clue how to add it with a dynamic position. 但我不知道如何用动态位置添加它。 Here's the code for adding the button: 这是添加按钮的代码:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
    action:@selector(buttonPressed:)
    forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"+1" forState:UIControlStateNormal];
    button.frame = CGRectMake(0.0f, 70.0f, 160.0f, 15.0f);
    [cell addSubview:button];

您可以随时检查单元格的高度,并按下这样的按钮框架。

button.frame=CGRectMake(0,cellHeight-15,160,15);

Set the autoresizing mask when adding the button. 添加按钮时设置自动调整遮罩。 I think the setting you want is: 我认为你想要的设置是:

button.autoresizingMask = UIViewAutoResizingFlexibleTopMargin;

Do this after adding the button to the cell. 将按钮添加到单元格后执行此操作。

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

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