ios - 为什么按钮背景图像会拉伸(stretch)?

标签 ios iphone uitableview adaptive-ui

我有一个应用程序,其中有一个侧边菜单。在侧面菜单中,其余按钮显示正常,但有两个按钮奇怪地拉伸(stretch)。 Screenshot附上屏幕截图。

这就是我设置按钮图像的方式。

- (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 。为什么会这样? update

最佳答案

我建议您使用 button.image 而不是 button.backgroundImage。看起来将 backgroundImage 设置为 UIButton 无法处理图像的 contentMode,因为它总是将 UIImage 拉伸(stretch)到与UIButton

如果您在 property image 上添加 UIImage,您可以在 UIButton 中更改图像的位置> 通过手动或在 Interface Builder 中更改 contentEdgeInsets

以防万一你想为你的UIButton设置一个UIImage和一个NSString,我会创建一个自己的类型类UIButtonUILabelUIImage 并将它们布局在 Interface Builder 中。

关于ios - 为什么按钮背景图像会拉伸(stretch)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27819937/

相关文章:

html - CSS - 变换 : rotateX animation not working on mobile browsers (even with -webkit-)

objective-c - 如何防止 uiwebview 缓存网页?

ios - 如何用另一个函数初始化一个类函数?

ios - 如何在父 View 上添加 subview Controller 的 View

iphone - NSLocale - 查找 CountryCode 然后使用它来部署代码

iphone - 如何在 iPhone (iOS) 上检测硬件键盘上的键盘事件

ios - 如何按 id 或月份 : Descending (Latest Post First)? 对数组进行排序

ios - 如何将 UITableView 中的行标记为 1、2、3 等

ios - Xamarin iOS - 在 ListView 行上选择打开 View Controller

iphone - XCode:如何以编程方式获取用户的电子邮件?