0

So guys,

I have a profile view that contains several informations. Some of them don't have to be set e.g. Biografie. So depending if its set, I would like to hide the Biografie View (checkout screenshot) and reposition the UserData View and the ActivityData View AND change the Size of their superview to fill the space made by the Biografie View.

Here is my actual structure of my TableViewHeader: enter image description here

The other point that i have to care about is that, the UILabel have to be mutliline and fit its content.

So my questions:

  • How do I reposition the UserData & ActivityData View to fill the space made by the hidden BiografieView?
  • How do I make the Biografie View height fit the UILabels text?

Please keep in mind that I'm using Autolayout. So I have to use constraints to modify the positions right?

To be honest.. Basically it should be like Instagram, when adding a Biografie. I hope I made it clear what I want..

Edit 1

I have tried your suggestion Damien, but nothing is changing:

    NSLog(@"Height %f, Width %f", self.labelBiografie.frame.size.height, self.labelBiografie.frame.size.width);

if ([self.profileUser objectForKey:kGSUserBiografieKey]) {
    self.labelBiografie.text = [self.profileUser objectForKey:kGSUserBiografieKey];
    [self.labelBiografie sizeToFit];
}else{
    [self.labelBiografie setHidden:YES];
    [self.countContainer setConstraintConstant:-self.labelBiografie.frame.size.height forAttribute:NSLayoutAttributeCenterY];
    [self.tableHeaderViewChildVIew setConstraintConstant:-self.labelBiografie.frame.size.height forAttribute:NSLayoutAttributeHeight];
    self.tableView.tableHeaderView = self.tableHeaderViewChildVIew;
}

EDIT 2:

Using this Project (UIView-UpdateAutoLayoutConstraints ) I'm getting following error on the methods:

Line 56: [self hideView:hidden byAttribute:NSLayoutAttributeHeight];

Line 85: [self setConstraintConstant:0 forAttribute:attribute];

Line 23:

[self.superview addConstraint: [NSLayoutConstraint constraintWithItem:self attribute:attribute relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:constant]];

ERROR:

    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x8c1b8e0 V:|-(0)-[UIButton:0x8c6fb40]   (Names: '|':UIView:0x8c6f630 )>",
    "<NSLayoutConstraint:0x8c19dc0 V:[UIButton:0x8c6fb40(100)]>",
    "<NSLayoutConstraint:0x8c1d740 V:[UIButton:0x8c6fb40]-(NSSpace(8))-[UILabel:0x8c70900]>",
    "<NSLayoutConstraint:0x8c20df0 V:[UILabel:0x8c70900]-(NSSpace(8))-[UIView:0x8c16a00]>",
    "<NSLayoutConstraint:0x8c20fa0 V:[UIView:0x8c16a00(40)]>",
    "<NSLayoutConstraint:0x8c23790 V:[UIView:0x8c16a00]-(0)-|   (Names: '|':UIView:0x8c6f630 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x8c78e60 h=--& v=--& V:[UIView:0x8c6f630(217)]>",
    "<NSLayoutConstraint:0x8c5d000 V:[UILabel:0x8c70900(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x8c20df0 V:[UILabel:0x8c70900]-(NSSpace(8))-[UIView:0x8c16a00]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

This error shows up on the Demo-Project out of the box, on my on Project it also get returned when i hide the view..

SaifDeen
  • 862
  • 2
  • 16
  • 33

2 Answers2

1

I built a category to answer to this problem I also already encountered : Hide autolayout UIView : How to get existing NSLayoutConstraint to update this one

EDIT:

If you use autolayout, do not use view.frame any more and don't forget to set all you autolayout view like this:

myView.translatesAutoresizingMaskIntoConstraints = NO;

Using this category https://github.com/damienromito/UIView-UpdateAutoLayoutConstraints

if(!user.biografie && user.biografie.lenght == 0)
{
     [biografieLabel setConstraintConstant:0 forAttribute:NSLayoutAttributeHeight];
}else
{
     biografieLabel.text = user.biografie;
}
Community
  • 1
  • 1
Damien Romito
  • 9,801
  • 13
  • 66
  • 84
  • Do you got a answer for Q2? – SaifDeen Apr 03 '14 at 16:58
  • Do you have just one UILabel in your biografie view? – Damien Romito Apr 03 '14 at 17:13
  • Damien I tried what you posted but it's not working none of the views are repositioning .. Any idea why? – SaifDeen Apr 04 '14 at 09:43
  • 1
    Are you sure to have positioned all your views with autolayout? if UserData & ActivityData views have frames, this is normal it's doesn't work. Don't forget this to set all your views as autolayout views : UserData.translatesAutoresizingMaskIntoConstraints = NO; ActivityData.translatesAutoresizingMaskIntoConstraints = NO; And above all, create constraints. http://enharmonichq.com/tutorial-auto-layout-part-2/ – Damien Romito Apr 04 '14 at 18:10
  • I set on all views translateAutoresizingMaskIntoConstraints to NO, but I made something wrong on the IB that cause this i think. Sorry but I think our timezones are really untimed :S I have to check it tommorow sorry for the waiting... – SaifDeen Apr 04 '14 at 19:58
  • Damien, I really need your help.. I tried severals variations of Constraints but nothing is working... Is it maybe because the views are inside a TableViewHeader? ... I'm getting nightmares from this shit... :( – SaifDeen Apr 05 '14 at 22:34
  • 1
    Ok, I use autolayout only programmatically. I will try to code an exemple for you (maybe this night). The tableviewheader isn't a problem. – Damien Romito Apr 06 '14 at 22:14
  • Wow... That would be awesome. Ty!! – SaifDeen Apr 07 '14 at 05:32
  • 1
    I code an exemple... i learnt some tricks cause the behavior you want is very complex. https://github.com/damienromito/UIView-UpdateAutoLayoutConstraints I implemented a method to hide/show a view. it's not work perfectly in this demo, but you can use it in other cases. – Damien Romito Apr 07 '14 at 23:02
  • Gonna try it out! Thankyou alot for the help! – SaifDeen Apr 08 '14 at 09:22
  • Damien, thx for the Demo-Project. The Constraints are perfect, but the hiding function returns a error. I'll update the question with details. Thx for helping me dude!! – SaifDeen Apr 08 '14 at 10:28
  • 1
    Yes, my bad, it's totally useless to set to "0" a constraint of a dynamic label (set label.text = @"") . I updated the demo. If all is ok for you, valid this answer please ;) – Damien Romito Apr 08 '14 at 17:15
  • Hey @Damien, could look on this question, too? It's a following question on this one: http://stackoverflow.com/questions/22983366/autolayout-uilabel-height-changes-on-second-view-appearence – SaifDeen Apr 10 '14 at 09:03
1

For your question two , the first solution would be to had a height constraint between the uilabel and its container's height(BiografieView) .

NSLayoutConstraint *constraint = [NSLayoutConstraint
                                  constraintWithItem:biografieView /// change here
                                  attribute:NSLayoutAttributeHeight
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:biografieLabel  /// change here
                                  attribute:NSLayoutAttributeHeight
                                  multiplier:1
                                  constant:0];
[view addConstraint:constraint];

Don't forget to set uilabel.numberOfLine = 0 , the height of this one will be automatically updated

BUT, BUT, BUT...* (better solution for your case) If you have only one uilabel in your biografie view, remove this container. The uilabel is also a uiview, and you will not have this problem any more.

Damien Romito
  • 9,801
  • 13
  • 66
  • 84