【问题标题】:Swift: layoutSubviews not being called by setNeedsLayoutSwift:setNeedsLayout 未调用 layoutSubviews
【发布时间】:2014-07-20 02:15:20
【问题描述】:

我有一个background,它也是一个UIView,其中有3 个UIViews 作为其子视图。 我为它们每个添加了一个UIPanGestureRecognizer,希望当用户向左拖动视图时,对应的UIView水平扩展。 这是我的代码:

func labelPanned(sender:UIPanGestureRecognizer!){
    self.labelBeingMoved = sender.view.tag
    if(sender.state == UIGestureRecognizerState.Began || sender.state == UIGestureRecognizerState.Changed){
        var translation:CGPoint = sender.translationInView(self.background)
        self.labelCurrWidth += translation.x
        NSLog("self.labelCurrWidth is now %f", self.labelCurrWidth)
        if(self.labelCurrWidth <= self.labelOriginalWidth || self.labelCurrWidth >= self.labelMaxWidth){self.labelCurrWidth -= translation.x}
        sender.setTranslation(CGPointZero, inView: self.background)
        dispatch_async(dispatch_get_main_queue(), {
            self.background.setNeedsLayout()
            })
    }
}

layoutSubviews() 电话中我有:

func layoutSubviews(){
    NSLog("layoutSubviews called")
    switch self.labelBeingMoved{
    case 1:
        NSLog("CASE 1")
        self.labelbg.frame = CGRectMake(self.labelbg.frame.origin.x, self.labelbg.frame.origin.y, self.labelCurrWidth, self.labelbg.frame.height)
    case 2:
        NSLog("CASE 2")
        self.label1bg.frame = CGRectMake(self.label1bg.frame.origin.x, self.label1bg.frame.origin.y, self.labelCurrWidth, self.label1bg.frame.height)
    case 3:
        NSLog("CASE 3")
        self.label2bg.frame = CGRectMake(self.label2bg.frame.origin.x, self.label2bg.frame.origin.y, self.labelCurrWidth, self.label2bg.frame.height)
    default:
        NSLog("An unknown error occurred")
    }
}

很简单,但不知何故它不起作用,我不知道出了什么问题。我也试过self.view.setNeedsLayout(),但也没有运气。 *labelbgUIViews,而不是 UILabels。

提前致谢!

【问题讨论】:

    标签: ios swift layoutsubviews


    【解决方案1】:

    你可能忘记在 layoutSubviews 方法前面添加 'override' 关键字

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 2011-02-18
      • 1970-01-01
      相关资源
      最近更新 更多