0

I have a bunch of questions:

  1. How do I position a UIView so that it is on the bottom of the view I am adding it to?
  2. How can I add a subview to a view so that it is positioned in the corner of the superview with a small gap (Like if I want a 'x' cross sign for closing something)
  3. Is their a utility class for easy UIView positioning (and rotation)?

Any references, open source tutorials etc. will be more then welcome!

s6luwJ0A3I
  • 1,023
  • 1
  • 9
  • 22

6 Answers6

1

(a) How do I position a UIView so that it is on the bottom of the view I am adding it to?

OK, let's say you want to position button as a subview at the bottom of view form, you calculate the origin.y of the subview button by subtracting button's height from the height of the form

CGRect buttonFrame = button.frame;
buttonFrame.origin.y = form.bounds.size.height - buttonFrame.size.height;
button.frame = buttonFrame;
[form addSubview:button];

You can change origin horizontal position as well. You want it on the bottom left of form?

buttonFrame.origin.x = 0;

Or on the right edge of form?

buttonFrame.origin.x = form.bounds.size.width - buttonFrame.size.width;

Or in the middle (horizontally) of form?

buttonFrame.origin.x = (form.bounds.size.width - buttonFrame.size.width) / 2;

or another way using CGRectGetMidX (found in CGGeometry utility methods):

buttonFrame.origin.x = CGRectGetMidX(form.bounds) - buttonFrame.size.width/2;

Autoresizing handles adjusting the frame when the parent view's size changes. But you still have to position it first.

progrmr
  • 75,956
  • 16
  • 112
  • 147
1
int xOffset = 20;
int yOffset = 20;
CGRect BottomRight_NewFrame = CGRectMake((superview.frame.size.width - subview.frame.size.width-xOffset), (superview.frame.size.height - subview.frame.size.height-yOffset), subview.frame.size.width, subview.frame.size.height);
subview.frame = BottomFrame;
Abdusha M A
  • 509
  • 5
  • 13
0

You can use the new Autolayout feature of iOS 6 or the old Struts & Springs in the Interface Builder to achieve this.

This tutorial explains both: http://msmvps.com/blogs/kevinmcneish/archive/2012/12/10/tutorial-ios-6-auto-layout-versus-springs-and-struts.aspx

Or you can set the autoresizing mask programatically. It is explained pretty well here: UIView autoresizingMask - Interface Builder to Code - Programmatically create struts and springs - Swift or Objective-C

Community
  • 1
  • 1
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
0

It's easy enough to just set the frame, e.g. (untested code )

subview.frame = CGRectMake((superview.frame.origin.x - subview.frame.origin.size.width/2)-20, (superview.view.frame.origin.y - subview.frame.origin.size.height/2)-20, subview.view.frame.size.width, subview.view.frame.size.height);

if you'll be doing a lot of this then create a utility class or method.

ader
  • 5,403
  • 1
  • 21
  • 26
0

Autolayout will help you position the views and maintain those positions if the size of the superview changes. If the superview isn't going to change, you don't really need to mess with constraints -- you can just set the position of the subview appropra

If you're adding view viewB to view viewA:

a) To position viewB so that it's bottom edge corresponds to the bottom edge of viewA:

viewB.frame.origin.y = viewA.bounds.size.height - viewB.bounds.size.height;

b) You don't say which corner, but it's just a matter of doing the math. For example, the upper right corner of viewA is at {viewA.bounds.size.x, 0} in viewA's coordinate system. If you want to put viewB there, set it's origin to:

{viewA.bounds.size.x-viewB.bounds.size.x, 0}

If you want to add a margin, you can add that to the computation:

int margin = 10;
{viewA.bounds.size.x-viewB.bounds.size.x-margin, margin}

d) Use NSLayoutConstraint to access the autolayout system's constraints programmatically. There's a nice visual format language, so that for your question (a) you could set the constraint for viewA to:

V:|-[viewB]-0-|

The V means that you're working in the vertical direction, |'s represent the edges (top and bottom, thanks to the V) of the superview (that's viewA), and the 0 means that the distance between viewB and the bottom of its superview should be 0.

Caleb
  • 124,013
  • 19
  • 183
  • 272
-1

You can setup constraints in iOS6 but if you want to work on older os's you need to position them manually. Math.

Dancreek
  • 9,524
  • 1
  • 31
  • 34