gigahari
gigahari

Reputation: 681

How do I programmatically create views based on device orientation in iOS?

I want to create many view's programmatically and set their size based on the device orientation. This is how I am doing it now:

UIView *view1 = [[UIView alloc] init];

if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) 
    view1.frame = CGRectMake (0, 0, 1024, 44);
else 
    view1.frame = CGRectMake (0, 0, 764, 44);

[self.view addSubview:view1];

But it is a pain to do this for each and every view. Also I have to duplicate the code I use in - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation) toInterfaceOrientation duration:(NSTimeInterval)duration method. Isn't there an easier way to do this?

Upvotes: 0

Views: 417

Answers (2)

Henry
Henry

Reputation: 479

Of Course you have.

the only thing you have to do is create a BaseViewController inherit from UIViewController

implement the methods to this base class, and all other ViewController you need inherit this BaseViewController

And you can also config UINavigationItems, UIActivityIndicators in this BaseViewController.

You have to adapt to the object oriented thinking :)

Upvotes: 1

J S Rodrigues
J S Rodrigues

Reputation: 471

Hey you dont have to do it. Set the AutoResizingMask for View, This is where you specify how diffent borders, height and width should reach to changes in parent views size.

Never try to change it manually. It will become complex.. Always make apple do it for you...

Upvotes: 0

Related Questions