如何在视图层次结构中找到布局?

时间:2018-01-19 12:54:49

标签: ios objective-c uitableview layout frame

我在列表视图控制器中有一个menuView。当单元格中的更多按钮被录音时,在UITableViewCell上添加了menuView。 enter image description here 我用单身人士达到了效果。

@implementation ProductsOperationMenu
static ProductsOperationMenu *_instance;
+ (instancetype)sharedInstance{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] initWithFrame:CGRectZero];
    });
    return _instance;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

ZBMyProductsCell.m

@implementation ZBMyProductsCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    _operationMenu = [[ProductsOperationMenu alloc] initWithFrame: CGRectZero];
}

- (IBAction)operationButtonClick:(UIButton *)sender {
    if ([self.contentView.subviews containsObject:_operationMenu]) {
        _operationMenu.hidden = ![_operationMenu isHidden];
    } else{
        [self.contentView addSubview:_operationMenu];
        _operationMenu.hidden = NO;
    }

    [_operationMenu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(205);
        make.height.mas_equalTo(60);
        make.bottom.mas_equalTo(self.operationButton).offset(0);
        make.right.mas_equalTo(self.operationButton.mas_left).offset(-10);
    }];
}

没有Singleton,就变成了这个:

enter image description here

所以问题来了。

我想把menuView放在控制器的视图上,因为它是唯一的或隐藏的,曾经属于单元格。

如何将所选更多按钮的布局转换为控制器视图? 如何使用这些方法来计算?

- convertPoint:toView:
- convertPoint:fromView:
......

我以简单的方式做到了。这是代码:

- (void)clickOperationButtonOfProductsCell:(ZBMyProductsCell *)myProductsCell{
    NSUInteger * operationIndex = [self.myProductsTableView.visibleCells indexOfObject:myProductsCell];
    CGFloat originY = operationIndex.row * 110 + 50 + 40;
    CGRect originFrame = CGRectMake(KScreenWidth - 55, originY, 0, 60);
    CGRect finalFrame = CGRectMake(KScreenWidth - 260, originY, 205, 60);
    self.operationMenuView.frame = originFrame;
    [UIView animateWithDuration: 0.5 delay: 0 options: UIViewAnimationOptionCurveEaseIn animations:^{
        self.operationMenuView.frame = finalFrame;
    } completion:^(BOOL finished) {    }];
}

如何更自适应地实现它?

3 个答案:

答案 0 :(得分:1)

也许你可以这样试试:

// here is your cell where the more button belongs to
@interface ZBMyProductsCell: UITableViewCell

@property (nonatomic, copy) void(^moreAction)(ZBMyProductsCell *cell);

@end

@implementation ZBMyProductsCell

- (void)_moreButtonClicked:(UIButton *)sender {
    !self.moreAction ?: self.moreAction(self);
}

@end

// here is your view controller where your table view belongs to
// this is a `UITableViewDataSource` delegate method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZBMyProductsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZBMyProductsCell" forIndexPath:indexPath];

    // Configure the cell...
    cell.moreAction = ^(ZBMyProductsCell *cell) {
        CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
        // write your own code to show/hide the menu
    };

    return cell;
}

答案 1 :(得分:0)

在名为cellIndexpath的每个单元模型中创建一个变量,在cellForRow中创建一个变量init

 cell.cellIndexpath  = indexpath

答案 2 :(得分:0)

看一下UIPopoverPresnetationController并查看它是否可以完成这项工作。它应该可以在iPhone上使用。将menu view放在controller’view上会在滚动表格视图时导致问题

使用

UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.detaiLabel.frame inView:self];