Can a standard accessory view be in a different position within a UITableViewCell?

前端 未结 11 1067
谎友^
谎友^ 2020-12-05 09:57

I want my accessory to be in a slightly different place than normal. Is it possible? This code has no effect:

cell.accessoryType =  UITableViewCellAccessoryD         


        
11条回答
  •  旧巷少年郎
    2020-12-05 10:12

    Another way to do this is to embed your custom accessory view in another view, that is set as the cell's accessory view and control the padding using the frame.

    Here is an example with an image view as custom accessory view:

    // Use insets to define the padding on each side within the wrapper view
    UIEdgeInsets insets = UIEdgeInsetsMake(24, 0, 0, 0);
    
    // Create custom accessory view, in this case an image view
    UIImage *customImage = [UIImage imageNamed:@"customImage.png"];
    UIImageView *accessoryView = [[UIImageView alloc] initWithImage:customImage];
    
    // Create wrapper view with size that takes the insets into account 
    UIView *accessoryWrapperView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, customImage.size.width+insets.left+insets.right, customImage.size.height+insets.top+insets.bottom)];
    
    // Add custom accessory view into wrapper view
    [accessoryWrapperView addSubview:accessoryView];
    
    // Use inset's left and top values to position the custom accessory view inside the wrapper view
    accessoryView.frame = CGRectMake(insets.left, insets.top, customImage.size.width, customImage.size.height);
    
    // Set accessory view of cell (in this case this code is called from within the cell)
    self.accessoryView = accessoryWrapperView;
    

提交回复
热议问题