Sunday 15 May 2011

ios - Setting Constraints programmatically different from setting them in IB? -


I'm new in the auto layout in IOS I really like the concept of the theory say, but to me easy task Trying crazy. I suspect that I still missing some simple underlying principles. I'm actually trying to learn and learn basic things before working with it in an app, so I'm making very easy test projects, it's just as simple as it does that does not work as expected. The first part that works in the IB, I add a view to fill the entire view controller, and XCode automatically sets the odds to top / bottom / leading / tracing and 0 in space. When done with IB, it works as expected:

Image details Enter here

 Enter image details here

Great!

Now I am trying to do this thing in code:

  - (zero) viewDidoadload {[Super Viewedload]; // Setup an additional after loading the view, usually from a nibb. UIView * redView = [[UIView alloc] initWithFrame: self.view.bounds]; RedView.backgroundColor = [UIColor redColor]; [Self.view addSubview: redView]; RedView.translatesAutoresizingMaskIntoConstraints = No; [Self.view addConstraint: [NSLayoutConstraint constraintWithItem: redView attribute: NSLayoutAttributeBottom related: NSLayoutRelationEqual toItem: self.view Feature: NSLoutoutbottom multiplier: 1.0f constant: 0.0F]]; [Self.view addConstraint: [NSLayoutConstraint constraintWithItem: redView feature: NSLayoutAttributeLeading Related: NSLayoutRelationEqual toItem: self.view feature: NSLayoutAttributeLeading multiplier: 1.0f constant: 0.0 F]]; [Self.view addConstraint: [NSLayoutConstraint constraintWithItem: redView feature: NSLayoutAttributeTrailing Related: NSLayoutRelationEqual toItem: self.view feature: NSLayoutAttributeBottom multiplier: 1.0f constant: 0.0f]]; [Self.view addConstraint: [NSLayoutConstraint constraintWithItem: redView feature: related NSLayoutAttributeTop: NSLayoutRelationEqual toItem: self.view feature: Anselautaitiling multiplier: 1.0 F constant: 0.0 F]]; }   

This is code other than the default code for a single code.

When I run the code above, I am trying to mirror similar things with the IB program, I get after rotation:

enter image description here

how it come that individual comes obstacles due to different results ? Maybe it's a really simple and shameful idiot I'm missing. << P>

Here's what I can to make it clear:

  [Self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "H: | [redView] |" | Options: 0 metric: zero views: NSDictionaryOfVariableBindings (redView)]]; [Self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "V: | [redView] |" | Options: 0 metric: zero views: NSDictionaryOfVariableBindings (redView)]]; AutoLayout in IB can be a pain and I think introducing myself with visual format language was less work than compulsion. Specialty: Related: Related: From: ITEM: Attribute: Multiplier: Continued:   

No comments:

Post a Comment