简体   繁体   English

在运行时具有自动布局的UIButton大小

[英]UIButton size with Autolayout at runtime

I have found several questions on this but I haven't found a solid answer on how to really do this, so I hope this isn't a duplicate just for not finding a concrete answer. 我已经找到了几个问题,但是对于如何真正做到这一点我还没有找到一个可靠的答案,所以我希望这不是因为没有找到具体答案而重复。

Here's my issue: I'm rewriting an old iOS app that has a tableview with custom cells, and inside each cell there's a button with a single color. 这是我的问题:我要重写一个旧的iOS应用程序,该应用程序具有带有自定义单元格的表格视图,并且在每个单元格内部都有一个具有单一颜色的按钮。 The width of the button is calculated at runtime to display each button's width being different for each cell, creating what looks like a side chart. 在运行时计算按钮的宽度,以显示每个按钮在每个单元格中的宽度,从而创建看起来像边图的按钮。

I want/need to do the same thing but I have my project already done with Autolayouts, and I need this to work on both iPhone and iPad so removing autolayout at this point will be kind of a pain. 我想要/需要做同样的事情,但是我的项目已经使用Autolayouts完成,并且我需要在iPhone和iPad上都可以使用它,因此在此时删除autolayout会很痛苦。

Because of the autolayout, I have the layout of my cell with constrains and it looks fine when all cells are the same width, but I haven't figured out a way to make it change it's width at runtime. 由于具有自动布局功能,因此我的单元格布局受到约束,并且当所有单元格的宽度相同时,它看起来还不错,但是我还没有找到一种在运行时更改其宽度的方法。

Here's also another issue, from the questions I have found on SO. 这也是我在SO上发现的问题中的另一个问题。

This is what my cell looks like at runtime: 这是我的单元在运行时的样子:

在此处输入图片说明

I need this button in the middle (the blue-ish thing) to stay in the middle of the team name and the value, and the values on the right have to remain in their same position (so not moving to the left if the bar is very small). 我需要在中间(蓝色的东西)处保留此按钮,以使其保持在团队名称和值的中间,并且右边的值必须保持在相同的位置(因此,如果条形图不能移到左边非常小)。

Any suggestion on this issue would be appreciated. 关于这个问题的任何建议将不胜感激。 Thank you! 谢谢!

Give the button both centerX and centerY constraints; 给按钮同时提供centerX和centerY约束; that will keep it in the middle. 它将保持在中间。 Give it a fixed width, and make an IBOutlet to that constraint so you can update its constant value in code to modify the width of the bar. 给它一个固定的宽度,并对该约束设置一个IBOutlet,以便您可以在代码中更新其常数值以修改钢筋的宽度。 The team name label should have a constraint to the left edge, and the value label should have one to the right edge; 团队名称标签的左边缘应具有约束条件,而值标签的右边缘应具有约束条件; that will keep them in place on all screen sizes. 这将使它们在所有屏幕尺寸上都保持不变。

As an example, this code in the table view controller, 例如,此代码在表格视图控制器中

- (void)viewDidLoad {
    [super viewDidLoad];
    self.theData = @[@{@"name":@"One", @"value": @6094, @"width": @200}, @{@"name":@"Two", @"value": @210, @"width": @20}, @{@"name":@"Three", @"value": @7075, @"width": @250}, @{@"name":@"Four", @"value": @6648, @"width": @225}, @{@"name":@"Five", @"value": @2300, @"width": @100}, @{@"name":@"Six", @"value": @900, @"width": @50}];
    [self.tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.theData.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    RDTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.leftLabel.text = self.theData[indexPath.row][@"name"];
    cell.rightLabel.text = [NSString stringWithFormat:@"%@",  self.theData[indexPath.row][@"value"]];
    cell.widthCon.constant = [self.theData[indexPath.row][@"width"] floatValue];
    return cell;
}

Gave this result, 得到这个结果,

在此处输入图片说明

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

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