Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set view position to top right programmatically in cocoa touch

I need to make sure that one view A (size: 200x200) is always aligned to top right corner inside second view B (full screen size). I want to make sure that view A stays in that place regardless of device orientation. Truth is I have no problem with this when using interface builder to position the views but I need to construct this programmatically. I suppose I should use some autoresizing settings, could you tell me which one is supposed to align the view to top right corner of its superview?

like image 354
mgamer Avatar asked Dec 14 '11 15:12

mgamer


1 Answers

UIView parentView  //your full screen view
UIView view //the 200x200 view

[parentView addSubview:view];
CGRect frame = view.frame;

//align on top right
CGFloat xPosition = CGRectGetWidth(parentView.frame) - CGRectGetWidth(frame);
frame.origin = CGPointMake(ceil(xPosition), 0.0);
view.frame = frame;

//autoresizing so it stays at top right (flexible left and flexible bottom margin)
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin

That positions the view on top right and set's the autoresizing mask so it stays at that position.

like image 107
V1ru8 Avatar answered Sep 30 '22 12:09

V1ru8