【问题标题】:Changing UILabel's text causes scrollview to jump更改 UILabel 文本导致滚动视图跳转
【发布时间】:2017-04-27 18:17:37
【问题描述】:

我正在使用计时器来更新 UILabel 的文本属性。 UILabel 在垂直的 UIScrollView 中。

static func play() {
        timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.timerUpdate), userInfo: nil, repeats: true)
        RunLoop.current.add(timer!, forMode: .commonModes)
    }

@objc private static func timerUpdate(_ timer: Timer) {
        let progress = AudioManager.currentTime
        NotificationCenter.default.post(name: NSNotification.Name("didUpdateProgress"), object: self, userInfo: ["progress":progress])
    }

当调用下一个函数时,currentTimeLabel.text 会更新为新字符串。

func didUpdateProgress(_ notification: Notification) {
        if let progress = notification.userInfo?["progress"] as? Float {
            slider.setValue(progress, animated: true)
            currentTimeLabel.text = String(format: "%01i:%02i", Int(progress/60), Int(progress) % 60)
        }
    }

但是如果scrollview不在它的初始位置,即contentOffset不为零,那么每次currentTimeLabel.text变化时,scrollView都会向上跳。我想,为这个特定标签显式设置高度约束会解决这个问题,但它没有:(

如何使用计时器更新 UIScrollView 中的 UILabel 文本,以使滚动视图不会跳转?

【问题讨论】:

  • 首先,您确定这是由设置标签文本引起的,而不是为滑块设置动画?
  • @DonMag 评论了更新 currentTimeLabel 修复跳跃的行
  • hmmm...必须在您的代码中发生其他事情...如果您注释掉 slider.setValue 行会发生什么?
  • @DonMag 滑块只是停止更新,但如果我不注释掉更新 currentTimeLabel =[,滚动视图仍会在其初始位置跳转
  • 您是否使用子分类标签在设置文本时执行其他操作?或者还有什么事情发生?显然,在 UIScrollView 中设置 UILabel 的文本导致滚动视图跳转是 not 设计的......

标签: ios swift uiscrollview uilabel


【解决方案1】:

我描述的问题在我的viewDidLayoutSubviews() 方法中。每次视图出现在屏幕上时,我都希望滚动视图重置其位置。所以我就这样做了

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    scrollView.setContentOffset(.zero, animated: false)
}

我不知道每次更改标签的文本属性时都会调用此方法。嗯,现在看来是绝对清楚了,因为改变文字会让标签重新计算它的宽度,但一开始并不是那么明显:)

抱歉,我错过了问题中的这一部分。尽量避免不必要的细节并使问题最小化,很容易忽略一些重要的事情,特别是如果你真的不知道问题出在哪里。

【讨论】:

  • 遇到了同样的情况。谢谢。
【解决方案2】:

我在使用 scrollView 时遵循这种做法。 如果您想使用 ScrollView,可以按照此操作。

  1. 使用scrollView并对superview(主视图)进行自动布局top、left、bottom、right 0约束

  2. 获取另一个视图,这将是您的容器视图,并按照上述方式进行自动布局(0 约束)

  3. 放置您想要的出口,并确保您已将所有最重要的顶部和底部约束拉到每个出口。如果需要高度,请给出具体高度。

  1. 这一点应该是一个。:) 把你的视野高度变大,这样你就可以放置你想要的插座,一切都会对你一目了然。

所以当我像这样写代码时,我没有发现任何异常。那是跳跃。应该再试一次。

class ViewController: UIViewController {
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
var timer:Timer!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    timer = Timer.scheduledTimer(timeInterval: 4.0, target: self, selector: #selector(self.onTimerUpdate), userInfo: nil, repeats: false)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func  onTimerUpdate() {
    self.label1.text = "I have changed"
}

}

我将滚动视图拉到底部,4 秒后标签文本已更改。

【讨论】:

    【解决方案3】:

    设置文本可能会改变 UILabel 的内在内容大小。如果 UILabel 属性 numberOfLines 定义为 0(默认),则允许根据内容增长。在这种情况下,UILabel 的高度可能会在设置文本时缩小/增长。如果您根据内在内容大小另外指定自动布局约束,则可能会影响滚动视图行为。我不太确定。

    因此请查看或提供其他信息:

    • UIScrollView 和 UILabel 自动布局约束
    • UILabel 属性 (numberOfLines=0?)

    【讨论】:

      猜你喜欢
      • 2011-07-16
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      相关资源
      最近更新 更多