0

I am working on autoLayout in iOS6. I wanted to reduce(minimize) size(frame) of UIView. Currently my view is 320X480. I wanted to make it 160X240 keeping center point as it is. Added NSLayoutConstraints (add vertical and horizonatal space to superview) and also took IBOutlets as follows :

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *rightSpace;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *bottomSpace;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *leftSpace;

Do I need to apply width and height constraints also? How should I get frame size of 160X240 (keeping center point same) using this? Or I am going wrong. Please guide me.

1
  • Do you want to change the view's size at run time, or are you just wanting to edit what you have?
    – jrturton
    Commented May 4, 2013 at 12:53

1 Answer 1

1

If you really want a fixed width and height using autolayout, then I think you need to add those constraints. In your storyboard you can pin the width and height of your view, hook them up to your controller as you did for the spacing above, and when you're ready set the size of the constraint. To keep the center point the same you may need to add another constraint in your storyboard to align your view with something else, but that depends on the other constraints.

With the constraints you have, instead of setting the height you could calculate it all manually and adjust each one individually - but the whole point of autolayout is to be able to describe the relations between your views in a way that will resolve to something no matter the size of the screen. With that goal I'd urge you to think about why you need it to be 160x240 and instead of using the fixed numbers, express the why of it as constraints.

Here is a screenshot showing a centered view with fixed height and width using autolayout: autolayout screenshot sample

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.