iphone - 如何在自定义单元格中居中放置图像

标签 iphone ios

在我的自定义单元格中,我想居中图像,但这不起作用。

    self.leftImage = [[[UIImageView alloc] initWithImage:cry] autorelease] ;
    float h=cell.frame.size.height;
    float w=cell.frame.size.width;

    CGRect r = leftImage.frame;
    r.origin = cell.frame.origin;
    r.origin.x = w/ 2  - r.size.width / 2;
    r.origin.y = h / 2  - r.size.height / 2 + 12;
    leftImage.frame = r;
            [self.contentView addSubview:leftImage];

最佳答案

我认为您尝试在创建新单元格时在- (UITableViewCell *)tableView:(UITableView *)tableViewRef cellForRowAtIndexPath:(NSIndexPath *)indexPath中执行此操作,或者在init节中尝试在单元格本身中执行此操作。目前,单元格没有正确的框架,您不能依靠它。

关于iphone - 如何在自定义单元格中居中放置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6979767/

相关文章:

iphone - 单击 iphone 中的选项卡栏项目时如何在不同类之间移动

swift - 我的 Xcode 10.0 找不到我的 Iphone SE,因为它运行的是 ios 12.4

iphone - 检测 iAd 何时不出现

iphone - 检测一个int是否连续重复

ios - 我可以仅共享我的 URL 并通过 In House Distribution 将应用程序无线下载到设备吗?

ios - UICollectionView 标题部分问题

ios - 健康工具包观察者查询总是在应用程序激活时调用

iphone - 通用应用程序中 iphone/ipad 的不同状态栏样式

iphone - CoreText 页面 View 示例 (CTPageViewer)

javascript - React Native UI 组件 : RCTBubblingEventBlock/RCTDirectEventBlock do not seem to work