How to pass EAGLView UIButton frame to viewcontroller

139 Views Asked by At

I have EAGLView and it's UIButton. In ViewController i need to display the same button in landscape mode with click event what it has in EAGLView. I tried to set [eaglView.save setFrame:CGRectMake(360, 10, 40, 40)]; but the size is not changing in landscape still it displaying portrait mode frame.

EAGLView.h:

@interface BooksEAGLView : UIView{

  UIButton *save;

 }
 @property(nonatomic, retain) UIButton *save;

EAGLView.mm:

 save=[UIButton buttonWithType:UIButtonTypeCustom];
 [save setImage:[UIImage imageNamed:@"share_1.png"] forState:UIControlStateNormal];
 [save setFrame:CGRectMake(240, 10, 40, 40)];
 [save addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
 [self addSubview:save];

Viewcontroller.mm:

 eaglView = [[BooksEAGLView alloc] initWithFrame:viewFrame delegate:self appSession:vapp];

 [self setView:eaglView];

 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)  interfaceOrientation duration:(NSTimeInterval)duration
 {


 if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){


 [eaglView.save setFrame:CGRectMake(360, 10, 40, 40)];



}else {

}

}

}
4

There are 4 best solutions below

0
Ashwinkumar Mangrulkar On

put the following code in viewcontroller in which you are calling your BooksEAGLView

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)  interfaceOrientation duration:(NSTimeInterval)duration
{


    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
        [eaglView.save setFrame:CGRectMake(360, 10, 40, 40)];

    }else {
         [eaglView.save setFrame:CGRectMake(240, 10, 50, 50)];
    }


}
0
Ashwinkumar Mangrulkar On

Please refer this code

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    eaglView = [[BooksEAGLView alloc] initWithFrame:CGRectMake(100, 100, 150, 200)];

    [self setView:eaglView];

}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)  interfaceOrientation duration:(NSTimeInterval)duration
{


    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
        [eaglView.save setFrame:CGRectMake(360, 10, 40, 40)];

    }else {
         [eaglView.save setFrame:CGRectMake(240, 10, 50, 50)];
    }


}
0
Ashwinkumar Mangrulkar On

BooksEAGLEView.h

#import <UIKit/UIKit.h>

@interface BooksEAGLView : UIView{

    UIButton *save;

}
@property(nonatomic, retain) UIButton *save;

@end
0
Ashwinkumar Mangrulkar On

BooksEAGLEView.m

#import "BooksEAGLView.h"

@implementation BooksEAGLView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        save=[UIButton buttonWithType:UIButtonTypeCustom];
        [save setFrame:CGRectMake(240, 10, 50, 50)];
        [save setBackgroundColor:[UIColor redColor]];
        [save setTitle:@"Save" forState:UIControlStateNormal];
        [save addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:save];
    }
    return self;
}
-(void)save:(id)sender
{
    NSLog(@"clicked...");
}