[英]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.