简体   繁体   English

自定义uitableviewcell重置子视图框架不起作用

[英]custom uitableviewcell reset subview frame doesn't work as I wanted

I custom a uitableviewcell with nib, and want to custom the highlighted style. 我用笔尖自定义了一个uitableviewcell,并且想要自定义突出显示的样式。 When the cell highlighted, I want to reduce the size of the subview of the cell and keep the position of the imageview in the cell not changing. 当单元格突出显示时,我想减小单元格子视图的大小,并保持imageview在单元格中的位置不变。 So, it looks like the frame of a imageview zoomed out, but the image itself stay there not changing its position. 因此,看起来图像视图的框架缩小了,但是图像本身停留在该位置,不会改变其位置。

However, this code snippet doesn't work as I wanted.Could anybody help me to figure out where I am wrong. 但是,此代码段无法按我的意愿工作,有人可以帮我弄清楚我哪里写错了。 Any help will be appreciated! 任何帮助将不胜感激!

tips: the imageview is a subview of self.frameView and frameView is a subview of frameBgView. 提示:imageview是self.frameView的子视图,frameView是frameBgView的子视图。

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if (highlighted)
    {
        self.frameView.backgroundColor = UIColorFromRGBA(0, 0, 0, 0.1);
        CGRect rect = self.frameBgView.frame;
        rect.origin.x += 10;
        rect.size.width -= 20;
        self.frameBgView.frame = rect;

        rect = self.frameView.frame;
        rect.origin.x -= 10;
        self.frameView.frame = rect;

    }
    else
    {
        ....

    }
}

EDIT: some screenshots to explain the question: 编辑:一些截图来解释这个问题: 单元格未突出显示

单元格突出显示

oky i tried your solution but i got like this i am posting the code as well as the output how it looks, if there is any problem just comment , i am deleted the frameBgView it is not required 好的,我尝试了您的解决方案,但是我像这样,我正在发布代码以及输出的外观,如果有任何问题,只是注释,我就删除了frameBgView这不是必需的

code

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
  {
    if (highlighted)
    {
      self.frameView.backgroundColor = [UIColor grayColor];
      CGRect rect = self.contentView.bounds;
      rect.origin.x += 15;
      rect.size.width -= 30;


    //rect = self.frameView.frame;
    // rect.origin.x -= 10;
      self.frameView.frame = rect;

   }
   else
  {
    self.frameView.backgroundColor = [UIColor whiteColor];
    CGRect rect = self.contentView.bounds;
    rect.origin.x += 10;
    rect.size.width -= 20;
    self.frameView.frame = rect;

  }

}

and i am not using auto layout .. the result what i got is, before 而且我没有使用自动布局..我得到的结果是,之前

选择之前

and after highlighted, 然后突出显示,

在此处输入图片说明

EDIT: 编辑:

this is the cell structure 这是细胞结构

在此处输入图片说明

as u can see in the picture, in content view i hav added frameView which is blue in colour, and within frameView i hav added imageView and also don't forget t set content mode scale to fill and also auto resizing masks for both frameView and imageView for example 如u可以在图片中看到,在content view我甲肝添加frameView其颜色为蓝色,并在frameView我甲肝添加imageView ,也不要算了笔设置内容模式scale to fill ,并自动调整大小口罩都frameViewimageView为例

autoresizing masks for content view 自动调整content view遮罩

在此处输入图片说明

autoresizing masks for frameView 自动调整frameView的蒙frameView

在此处输入图片说明

autoresizing masks for imageView 在自动口罩imageView

在此处输入图片说明

END EDIT 结束编辑

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

相关问题 UITableViewCell和在layoutSubviews中设置子视图框架第一次不起作用 - UITableViewCell and setting subview frames in layoutSubviews doesn't work first time UIButton 不适用于自定义 UITableViewCell (Swift) - UIButton doesn't work on a custom UITableViewCell (Swift) 自定义 UITableViewCell 自动布局不起作用 - Custom UITableViewCell auto layout doesn't work 未显示将子视图添加到UITableViewCell - Adding a SubView to UITableViewCell doesn't get displayed UITableViewCell中的UIButton子视图未按预期隐藏 - UIButton subview in UITableViewCell doesn't hide as expected 自定义 UITableViewCell - 似乎无法添加子视图 - Custom UITableViewCell - can't seem to add subview 自定义UIView(.xib)在UITableViewCell中添加为子视图后自动更改框架 - Custom UIView(.xib) changes frame automatically after adding as subview in UITableViewCell 我第一次设置后,UITableViewCell框架没有改变 - UITableViewCell Frame Doesn't change after i set in first load tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath)将UITapGestureRecognizer添加到自定义UITableViewCell时不起作用 - tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) Doesn't work when I add a UITapGestureRecognizer to my custom UITableViewCell 自定义UITableViewCell删除子视图 - Custom UITableViewCell Removing a subview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM