0

Here is my tableview row/cell:

enter image description here

there are constraints set in place - the imageview is below the label and the button is below the imageview.

here is my code:

if(row == 1) {
    imageview.hidden = false
} else {
    imageview.hidden = true
}

how can i change the button constraint from below imageview to below label?

1
  • Could you please frame your question in a better way? I can't understand what you wish to achieve from the question in the comment. Also, dont ask question in comment and code part. Commented Jul 1, 2016 at 19:04

5 Answers 5

2

Adding and removing constraints is really bad example for that. I'll make your UI more complex.

Best way of solving these auto-layout problems is adding two constraints. One from imageView to button and second from imageView to label.

Now after setting these constraints, you need to set their priority levels. So, let's say button will be below the imageView first. In this case, you need to set imageView to button constraint's priority to something like 750 or UILayoutPriorityDefaultHigh and label to button constraint's priority to 250 or UILayoutPriorityDefaultLow.

Let's start creating a custom UITableViewCell

class YourTableViewCell: UITableViewCell {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var buttonToLabelConstraint: NSLayoutConstraint!
    @IBOutlet weak var buttonToImageViewConstraint: NSLayoutConstraint!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    func shouldHideImageView(hidden: Bool) {
        if(hidden == false) {
          buttonToLabelConstraint.priority = UILayoutPriorityDefaultLow
          buttonToImageViewConstraint.priority = UILayoutPriorityDefaultHigh
          imageView.hidden = true
        } else {
          buttonToLabelConstraint.priority = UILayoutPriorityDefaultHigh
          buttonToImageViewConstraint.priority = UILayoutPriorityDefaultLow
          imageView.hidden = false
        }
        self.contentView.layoutIfNeeded()
    }
}

After that, in your class where tableView is placed implement a logic like that:

if(row == 1) {
  cell.shouldHideImageView(true)
} else {
  cell.shouldHideImageView(false)
}

You should be all set.

Sign up to request clarification or add additional context in comments.

10 Comments

how do i create two constraints and call them?
you can create them in Interface Builder or programmatically. Which way do you prefer?
I can't teach you. But you can google it. If you don't know how to create auto layout constraint then follow some tutorials. I can provide you two links. Working Auto layouts in Interface builder from here code.tutsplus.com/tutorials/… and programmatically form here makeapppie.com/2014/07/26/… . If you follow these tutorials, you can learn them.
yes i know how to make constraints with interface builder but how do i make 2 sets of constraints and then call each one in the different if else statement?
Create these constraints in Interface builder. Set their default priorities in Interface Builder as I described. Then create IBOutlet properties for those constraints. Then, you can access them in if else statement.
|
1

You can try using a StackView, when you tell something to be hidden, the imageView the stack view will adjust the StackView as if the imageView was never a part of the view and it is an easy work around to not have to worry about constraints.

2 Comments

How do you do that and also what are the down falls?
First Add a vertical stack view form the object library and constrain it to your cell. Then throw in the two labels and image in the order you desire, and you can set sizes in the distribution section in the attributes inspector. Once you get it how you want, you just tell the image view to be hidden and the stack view will collapse and you will have two label one right above the other.
0

You can create IBOutlet on constraint and then just simply change the value like this:

buttonConstraint.constant = newValue

But i suggest you create for this a tableView. In this case you code and logic, i think, will be more accurate.

Comments

0

you could to this instead of hiding.

  1. Make an outlet from the heights constraint of the imageview, call it constraint for now.
  2. Set constraint.constant = 0 // effectively same as hiding.
  3. Set constraint.constant = NON_ZERO_VALUE // effectively same as show.

hope it helps!

Comments

0

I see a couple of options. The first is a little easier to implement but a little less flexible if you decide to change your layout later.

  1. Make the button's constraint to be below the label. Keep a reference to this constraint (you can connect it to your code via storyboard just like you do with the button itself, if you're using storyboard). When the imageView is visible, set myConstraint.constant += myImageView.frame.height. When the imageView is hidden, set myConstraint.constant -= myImageView.frame.height. Afterwards, call view.setNeedsLayout to update your constraints.

  2. Make two constraints: one for below the image, and one for below the label ("constraintToImage" and "constraintToLabel"). Hook them both up to your controller like in option 1, and call view.addConstraint(constraintToImage) and view.removeConstraint(constraintToLabel) when the image becomes visible (and the opposite for when it's hidden). Again, call view.setNeedsLayout after.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.