2016-08-30 4 views
1

У меня есть расширение клавиатуры, которое начинается с постоянной высоты 280. Я даю ему ограничение по высоте с помощью кода ниже. Я называю это в viewWillAppear.Изменение высоты расширения клавиатуры динамически

func normalHeight() { 
    constant = 280 
    UIView.animateWithDuration(1.0) { 
     let heightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: self.constant) 
     heightConstraint.priority = self.normalHeightPriority 
     self.view.addConstraint(heightConstraint) 
    } 
    self.view.setNeedsUpdateConstraints() 
    self.view.setNeedsDisplay() 

} 

Я также оживляю увеличение ограничения высоты с помощью кода, ниже которого также работает. Высота увеличивается.

func extendedHeight() { 
    constant = 400 
    UIView.animateWithDuration(1.0) { 
     let heightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: self.constant) 
     heightConstraint.priority = self.extendedHeightPriority 
     self.view.addConstraint(heightConstraint) 
    } 
    self.view.setNeedsUpdateConstraints() 
    self.view.setNeedsDisplay() 
} 

Чтобы вернуть высоту, я снова вызываю normalHeight() с кнопки, и это не работает. Я подумал, что, возможно, приоритетными были проблемы, поскольку ограничение extendeHeight имеет приоритет 1000, а ограничение normalHeight имеет приоритет 999. Поэтому я перехожу к приоритетам, когда функции вызываются, как показано ниже. Еще нет кубиков.

func normalHeight() { 
    constant = 280 
    normalPriority = 1000 
    extendedPriority = 999 
    UIView.animateWithDuration(1.0) { 
     let heightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: self.constant) 
     heightConstraint.priority = self.normalPriority 
     self.view.addConstraint(heightConstraint) 
    } 
    print("Normal Height Called") 
    self.view.setNeedsUpdateConstraints() 
    self.view.setNeedsDisplay() 

} 

и на расширенном приоритете, как так

func extendedHeight() { 
    constant = 400 
    extendedPriority = 1000 
    normalPriority = 999 
    UIView.animateWithDuration(1.0) { 
     let heightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: self.constant) 
     heightConstraint.priority = self.extendedPriority 
     self.view.addConstraint(heightConstraint) 
    } 
    self.view.setNeedsUpdateConstraints() 
    self.view.setNeedsDisplay() 
} 

Я буду признателен за любую помощь и понимание, почему это не работает. Спасибо

ответ

2

Я нашел решение. Я добавил два ограничения по высоте для представления и дал им приоритеты высокого и низкого уровня. Приоритеты ограничения должны быть высокими и низкоприоритетными, что составляет 750 и 250 соответственно вместо 1000 и 999, которые я использовал ранее.

Теперь, когда я хочу изменить высоту, я переворачиваю приоритеты двух установленных ограничений.

func initialHeight() { 
    AConstant = 280 
    BConstant = 400 
    self.aheightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: self.AConstant) 
    self.aheightConstraint!.priority = 750 
    self.view.addConstraint(self.aheightConstraint!) 

    self.bheightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: self.BConstant) 
    self.bheightConstraint.priority = 250 
    self.view.addConstraint(bheightConstraint) 

    self.view.setNeedsDisplay() 

Для расширенной высоты я называю

func extendedHeight() { 
    bheightConstraint.priority = 750 
    aheightConstraint.priority = 250 
    view.setNeedsLayout() 

} 

и вернуться к нормальной высоте я называю

func normalHeight() { 
    bheightConstraint.priority = 250 
    aheightConstraint.priority = 750 
    view.setNeedsLayout() 
} 
Смежные вопросы