3

I need to change the position of a label inside a Viewcontroller that uses autolayout. Before to do this I used

[arriveTimeLabel setTranslatesAutoresizingMaskIntoConstraints:YES];
[departTimeLabel setTranslatesAutoresizingMaskIntoConstraints:YES];

but that yields some runtime warnings

 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.
2013-08-15 23:25:58.791 Vicinitime[78327:c07] Unable to simultaneously satisfy constraints.
    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:0x9173a20 UIScrollView:0x9580190.bottom == UIView:0x9173890.bottom>",
    "<NSLayoutConstraint:0x9171fe0 V:|-(138)-[UILabel:0x958cf70]   (Names: '|':UIScrollView:0x9580190 )>",
    "<NSLayoutConstraint:0x91739a0 V:|-(0)-[UIScrollView:0x9580190]   (Names: '|':UIView:0x9173890 )>",
    "<NSAutoresizingMaskLayoutConstraint:0xab6ec90 h=--& v=&-& UILabel:0x958cf70.midY == -6.3989*UIScrollView:0x9580190.height>",
    "<NSAutoresizingMaskLayoutConstraint:0xab6eef0 h=--& v=&-& V:[UILabel:0x958cf70(25)]>",
    "<NSAutoresizingMaskLayoutConstraint:0xab6dbd0 h=--& v=--& V:[UIView:0x9173890(455)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x9171fe0 V:|-(138)-[UILabel:0x958cf70]   (Names: '|':UIScrollView:0x9580190 )>

So I looked around and saw that to fix that problem I have to set [arriveTimeLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; [departTimeLabel setTranslatesAutoresizingMaskIntoConstraints:NO];

to no. The problem is that now the labels arn't changing position programmatically.

If you need to code to set the labels programatically:

    if(travelTimeInt <= activityTimeInt){

        [timeBreakdownButtonImage setBackgroundImage:[UIImage imageNamed:@"timeBreakdownBackground7.png"] forState:UIControlStateNormal];
        NSLog(@"got to setting label place");

        [self.arriveTimeLabel  setFrame:CGRectMake(67, 170, 41, 25)];

        [self.departTimeLabel  setFrame:CGRectMake(211, 170, 41, 25)];



    }
    if(travelTimeInt * 7 >= activityTimeInt){

            [timeBreakdownButtonImage setBackgroundImage:[UIImage imageNamed:@"timeBreakdownBackground6.png"] forState:UIControlStateNormal];

          NSLog(@"got to setting label place");

            self.arriveTimeLabel.frame = CGRectMake(71, 170, 41, 25);

       self.departTimeLabel.frame = CGRectMake(207, 170, 41, 25);

    }

So is there a way to change the position of uilabel with autolayout on? maybe disable autolayout?

Spenciefy
  • 892
  • 11
  • 33
  • 1
    Disabling autolayout option individually for each component is not possible as of now. – Puneet Sharma Aug 16 '13 at 06:32
  • What about disabling for the entire viewcontroller? – Spenciefy Aug 16 '13 at 06:33
  • Yes ofcourse you can disable it for whole ViewController. Select the nib associated with your ViewController -> Go to File Inspector -> Uncheck "Use Autolayout" in Interface Builder Document Section. – Puneet Sharma Aug 16 '13 at 06:58
  • I am using storyboard so if i disable it then it disables for entire storyboard – Spenciefy Aug 16 '13 at 07:05
  • 1
    You need to create a separate storyboard with Autolayout unchecked, and move all of your without Autolayout ViewControllers there and then simply link your storyboards. – Puneet Sharma Aug 16 '13 at 07:12

3 Answers3

4

If you're using Autolayout then your UIViews are positioned based on their NSLayoutConstraints.

To move them, rather than set their frames you need to alter the constraints.

Mike Pollard
  • 10,195
  • 2
  • 37
  • 46
1

Disabling autolayout for a subview can be done as mentioned here: Can I disable autolayout for a specific subview at runtime?

The other option would be to create two storyboards, one with auto-layout on, and other with auto-layout off. Then load the respective storyboard by checking the Device System Version(assuming you are targeting iOS versions earlier than 6 too). For this, add this macro to your AppDelegate:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:(v) options:NSNumericSearch] != NSOrderedAscending)

and then:

UIStoryboard *mainStoryboard = nil;

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) 
{
     mainStoryboard = [UIStoryboard storyboardWithName:@"AutoLayoutStoryboard" bundle:nil];
} 
else 
{
     mainStoryboard = [UIStoryboard storyboardWithName:@"NoAutoLayoutStoryboard" bundle:nil];
}

//load initial view controller
UIViewController *rootView = [mainStoryboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = rootView;
[self.window makeKeyAndVisible];
Community
  • 1
  • 1
instaable
  • 3,449
  • 2
  • 24
  • 31
0

I'm working on a project that was initiated with auto layout, but I needed to dynamically adjust some frames in code. This is what I did to not be impaired by automatically generated run time constraints (courtesy of auto layout):

1) Do NOT have views or components laid out in interface builder.

2) Add your views purely programmatically starting with alloc/init and setting their frames appropriately.

3) Done.

Hope that helps!

ps. you can also experiment with stripping constraints from views with:

[view removeConstraints:view.constraints] but i've had more luck with the pure code approach.

Yup.
  • 1,883
  • 21
  • 18