Sagar Mohanty
Sagar Mohanty

Reputation: 237

How do i refresh a UIView when Orientation happens?

My code goes like this:

- (void)viewDidLoad
{
  [self setGridView];
}
-(void)setGridView
{
  CGRect frame; 
  frame .origin.x=0;
  frame.origin.y=20;
  frame.size.width=GRID_WEIGHT;
  frame.size.height=GRID_HEIGHT;
  GridView *ObjGridView=[[GridView alloc]initWithFrame:frame]; 

  [[NSBundle mainBundle ] loadNibNamed:@"GridView" owner:ObjGridView options:nil];
  [ObjGridView setGridViewFrame:frame];

  [self.view addSubview:ObjGridView.GridCellView];
  frame .origin.x+=GRID_WEIGHT;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return YES;
}

This code adds a subview to a view and sets the frame

My problem: 1-How do i refresh my view when Orientation(landscape or portrait) happens, because i set the frame of subview in the lanscape mode and i wants to use the sane view in my portrait view also .(basically where do i call this -(void)setGridView delegate method)?

2-How do i know, my subview exceeding the bound of the view,so that i can handle the subview in my setGridView method ?

Upvotes: 3

Views: 6461

Answers (5)

Smit Saraiya
Smit Saraiya

Reputation: 391

In viewDidLoad

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

Method for notify you that Orientation Changed:-

-(void)OrientationChange:(NSNotification*)notification
{
    UIDeviceOrientation Orientation=[[UIDevice currentDevice]orientation];

    if(Orientation==UIDeviceOrientationLandscapeLeft || Orientation==UIDeviceOrientationLandscapeRight)
    {
        NSLog(@"Landscape");
    }
    else if(Orientation==UIDeviceOrientationPortrait)
    {
        NSLog(@"Portrait");
    }
}

Upvotes: 1

SimplGy
SimplGy

Reputation: 20437

I found this question while looking for a way to react to orientation change inside the UIView itself. In case anyone else comes along...

If you want to react to orientation change inside a UIView, rather than a UIViewController (for encapsulation or other reasons), you can use this method:

class MyView: UIView {
  override func layoutSubviews() {
    super.layoutSubviews()
    println("orientation or other bounds-impacting change")
  }
}

Upvotes: 0

Damitha Raveendra
Damitha Raveendra

Reputation: 1721

1.Below method will call automatically whenever your orientation changes. Do the necessary changes according to each orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

if (interfaceOrientation == UIInterfaceOrientationPortrait) {}

else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {} 

else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {} 

else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {}

return YES;

}

2.You should know the width and height of your views and set the frames accordingly. That is not a big deal.

Hope this is helpful.

Upvotes: 2

railwayparade
railwayparade

Reputation: 5156

In response to your questionsL

With regard to resizing on orientation change: If you set your springs and struts accordingly it should autoresize automatically, alternatively you can do this in code as per deamonsarea's answer

To check if a view is exceeding the bounds of the superview use CGRectContainsRect something like.

CGRect frame0 = self.view.bounds;
CGRect frame1 = ObjGridView.frame;
if(CGRectContainsRect(frame0,frame1)==NO){
  NSLog(@"exceeds bounds")
}

Also noticed you are not calling [super viewDidLoad] and this line

[[NSBundle mainBundle ] loadNibNamed:@"GridView" owner:ObjGridView options:nil];

loads a new instance of the view but you are not refering to it anywhere

Upvotes: 0

Anil
Anil

Reputation: 2554

I am learning the ins and outs of iOS Application development myself, so please forgive me for the brevity of my response.

I believe you may be able to find the answer to your issue within the section titled 'Responding to Orientation Changes' within this document on Apple's Developer Resources:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html

I hope this helps you deduce a resolution to your issue.

Upvotes: 1

Related Questions