我有一个应用程序,其中有一个侧边菜单。在侧面菜单中,其余按钮显示正常,但有两个按钮奇怪地拉伸(stretch)。 附上屏幕截图。
这就是我设置按钮图像的方式。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
RearViewTableViewCell *cell = [self.tableViewSideMenu dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(cell == nil)
{
NSArray *cellView = [[NSBundle mainBundle] loadNibNamed:@"RearViewTableViewCell" owner:nil options:nil];
cell = (RearViewTableViewCell *)[cellView objectAtIndex:0];
cell.btnItemSelector.tag = indexPath.row;
[cell.btnItemSelector setBackgroundImage:[UIImage imageNamed:[buttonUnselect objectAtIndex:indexPath.row]] forState:UIControlStateNormal];
[cell.btnItemSelector setBackgroundImage:[UIImage imageNamed:[buttonSelect objectAtIndex:indexPath.row]] forState:UIControlStateHighlighted];
[cell.btnItemSelector addTarget:self action:@selector(btnMenuItemTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.selectionStyle =UITableViewCellSelectionStyleNone;
}
return cell;
}
我是自适应布局的新手。它会引起问题吗?在 iphone 5s 上它运行良好,但在 iphone 6 上它描述了这种行为。我只向 TableView 添加了一个约束(宽度)。我在这里使用的 uitableviewcell(自定义)具有前导空间、垂直空间、中心对齐等所有常规约束。有什么想法吗?
更新:我将 bg 颜色设置为红色,结果问题中的两个按钮被调整为更小且可能更宽的 View 。为什么会这样?
最佳答案
我建议您使用 button.image
而不是 button.backgroundImage
。看起来将 backgroundImage 设置为 UIButton
无法处理图像的 contentMode
,因为它总是将 UIImage
拉伸(stretch)到与UIButton
。
如果您在 property
image
上添加 UIImage
,您可以在 UIButton
中更改图像的位置> 通过手动或在 Interface Builder
中更改 contentEdgeInsets
。
以防万一你想为你的UIButton
设置一个UIImage
和一个NSString
,我会创建一个自己的类型类UIButton
与 UILabel
和 UIImage
并将它们布局在 Interface Builder
中。
关于ios - 为什么按钮背景图像会拉伸(stretch)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27819937/