繁体   English   中英

以编程方式将UIButton添加到UITableView错误的位置

[英]Adding UIButton To UITableView Programmatically Wrong Spot

我试图将UIButton添加到TableView,它位于表视图的右下角,从右边缘起20,从底部起20。 但是,最终将按钮粘在左上角。 我究竟做错了什么?

UIButton *goToTop = [UIButton buttonWithType:UIButtonTypeCustom];
    [goToTop setImage:redGo forState:UIControlStateNormal];
    [goToTop addTarget:self action:@selector(beginCampaign) forControlEvents:UIControlEventTouchUpInside];
    [goToTop setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [goToTop.layer setBorderColor:[[UIColor redColor] CGColor]];
    [self.tableView addSubview:goToTop];

    goToTop.translatesAutoresizingMaskIntoConstraints = NO;
    /* Leading space to superview */
    NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
                                                 constraintWithItem:goToTop attribute:NSLayoutAttributeRight
                                                 relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:
                                                 NSLayoutAttributeLeft multiplier:1.0 constant:20];
    /* Top space to superview Y*/
    NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint
                                                 constraintWithItem:goToTop attribute:NSLayoutAttributeBottom
                                                 relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:
                                                 NSLayoutAttributeTop multiplier:1.0f constant:20];
    /* Fixed width */
    NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:goToTop
                                                                       attribute:NSLayoutAttributeWidth
                                                                       relatedBy:NSLayoutRelationEqual
                                                                          toItem:nil
                                                                       attribute:NSLayoutAttributeNotAnAttribute
                                                                      multiplier:1.0
                                                                        constant:60];
    /* Fixed Height */
    NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:goToTop
                                                                        attribute:NSLayoutAttributeHeight
                                                                        relatedBy:NSLayoutRelationEqual
                                                                           toItem:nil
                                                                        attribute:NSLayoutAttributeNotAnAttribute
                                                                       multiplier:1.0
                                                                         constant:60];
    /* 4. Add the constraints to button's superview*/
    [self.tableView addConstraints:@[leftButtonXConstraint, leftButtonYConstraint, widthConstraint, heightConstraint]];

这是我想要的:

在此处输入图片说明

看起来是这样的:

在此处输入图片说明

您已将约束设置为表格视图的左上方,而不是右下方。 更改此:

/* Leading space to superview */
NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
                                             constraintWithItem:goToTop attribute:NSLayoutAttributeRight
                                             relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:
                                             NSLayoutAttributeLeft multiplier:1.0 constant:20];
/* Top space to superview Y*/
NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint
                                             constraintWithItem:goToTop attribute:NSLayoutAttributeBottom
                                             relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:
                                             NSLayoutAttributeTop multiplier:1.0f constant:20];

对此:

/* Leading space to superview */
NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
                                             constraintWithItem:goToTop attribute:NSLayoutAttributeRight
                                             relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:
                                             NSLayoutAttributeRight multiplier:1.0 constant:20];
/* Top space to superview Y*/
NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint
                                             constraintWithItem:goToTop attribute:NSLayoutAttributeBottom
                                             relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:
                                             NSLayoutAttributeBottom multiplier:1.0f constant:20];

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM