gpt4 book ai didi

ios - 如何正确地将 UIView 连续移动到 UIButtons 下面

转载 作者:行者123 更新时间:2023-11-29 11:26:58 27 4
gpt4 key购买 nike

我正在制作一种标签栏,其下方有一个指示器,指示选择了哪个标签。我的 View 层次结构非常简单:

---------------------------------
|------------- -------------|
|| | | ||
|| 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)>

我真的不明白为什么以这种方式移动 View 会对我的两个按钮之间的水平间距约束产生任何影响。

编辑这是最终产品:

enter image description here

最佳答案

重新排列代码,如下所示。我认为它会解决这个问题。尝试过后请告诉我。

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

关于ios - 如何正确地将 UIView 连续移动到 UIButtons 下面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57785365/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com