Let's do something really simple to get to grips with positioning a view programmatically for any size screen in iOS.
Here's some code that can be placed inside the viewDidLoad of your initial view controller.
First we create the view, which is relative in size and position to the frame of the view controller's main view.
UIView *blue = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*0.25, CGRectGetHeight(self.view.frame)*0.25, CGRectGetWidth(self.view.frame)/2, CGRectGetHeight(self.view.frame)/2)];
Second, so that we know it's there, we add some colour.
blue.backgroundColor = [UIColor blueColor];
Finally because we want the view to remain the same relative size and relative position in any orientation we throw the whole works at the autoresizingMask.
blue.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
Now we're ready to add the subview to the view hierarchy.
[self.view addSubview:blue];
And that's it.
Comments
Post a Comment