如何在 UIButtons 下连续正确移动 UIView
Posted
技术标签:
【中文标题】如何在 UIButtons 下连续正确移动 UIView【英文标题】:How to correctly move a UIView below UIButtons in a row 【发布时间】:2019-09-04 09:23:57 【问题描述】:我正在制作一种标签栏,其下方有一个指示符,指示选择了哪个标签。我的视图层次结构非常简单:
---------------------------------
|------------- -------------|
|| | | ||
|| Button 1 | | Button 2 ||
||____________| |___________||
| ------------ |
---------------------------------
我最初将指标前导和尾随约束设置为与按钮 1 相同:
self.indicatorLeadingConstraint = [self.indicatorView.leadingAnchor constraintEqualToAnchor:self.firstButton.leadingAnchor];
self.indicatorLeadingConstraint.active = YES;
self.indicatorTrailingConstraint = [self.indicatorView.trailingAnchor constraintEqualToAnchor:self.firstButton.trailingAnchor];
self.indicatorTrailingConstraint.active = YES;
然后我会在点击按钮时更新它们:
self.indicatorLeadingConstraint.active = NO;
self.indicatorLeadingConstraint = [self.indicatorView.leadingAnchor constraintEqualToAnchor:button.leadingAnchor];
self.indicatorLeadingConstraint.active = YES;
self.indicatorTrailingConstraint.active = NO;
self.indicatorTrailingConstraint = [self.indicatorView.trailingAnchor constraintEqualToAnchor:button.trailingAnchor];
self.indicatorTrailingConstraint.active = YES;
这实际上工作正常,但是我在控制台中收到警告:
(
"<NSLayoutConstraint:0x600001332620 H:[UIButton:0x7ffa68556530'First (0)']-(16)-[UIButton:0x7ffa6865e0f0'Second (0)'] (active)>",
"<NSLayoutConstraint:0x600001318a00 IndicatorView.leading == UIButton:0x7ffa6865e0f0'Second (0)'.leading (active, names: IndicatorView:0x7ffa6865eb20 )>",
"<NSLayoutConstraint:0x600001332760 IndicatorView.trailing == UIButton:0x7ffa68556530'First (0)'.trailing (active, names: IndicatorView:0x7ffa6865eb20 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600001332620 H:[UIButton:0x7ffa68556530'First (0)']-(16)-[UIButton:0x7ffa6865e0f0'Second (0)'] (active)>
我真的不明白为什么以这种方式移动视图会对我的两个按钮之间的水平间距约束产生任何影响。
编辑这是最终产品:
【问题讨论】:
Offtop:试试github.com/SnapKit/SnapKit/… 【参考方案1】:重新排列代码,如下所示。我认为这将解决问题。试过后告诉我。
IndicatorLeadingConstraint.isActive = false
IndicatorTrailingContraint.isActive = false
IndicatorLeadingConstraint = indicatorView.leadingAnchor.constraint(equalTo: sender.leadingAnchor)
IndicatorTrailingContraint = indicatorView.trailingAnchor.constraint(equalTo: sender.trailingAnchor)
IndicatorTrailingContraint.isActive = true
IndicatorLeadingConstraint.isActive = true
【讨论】:
完美,一看到你的回答,我就想“当然!”以上是关于如何在 UIButtons 下连续正确移动 UIView的主要内容,如果未能解决你的问题,请参考以下文章
第三个和其他连续的 UIButtons 不会出现在 UIScrollView