how can i resize frame of UIViewController when rotate screen? - iphone

I'm developing a program based iPad.
I created new modal UIViewController as formsheet mode not full screen.
And to resize the view, i used following codes.
...
MyController* controller = [[MyController alloc] init];
controller.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController : controller animated: YES];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if ( UIInterfaceOrientationIsPortrait(orientation) )
controller.view.superview.frame = CGRectMake(200, 100, 350, 600);
else
controller.view.superview.frame = CGRectMake(100, 200, 600, 350);
...
in MyController, i taked following codeds as didRotateFromInterfaceOrientation.
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if ( fromInterfaceOrientation == orientation )
return;
if ( UIInterfaceOrientationIsPortrait(orientation) )
{
self.view.frame = CGRectMake(0, 0, 350, 600);
self.view.superview.frame = CGRectMake(200, 100, 940, 628);
}
else
{
self.view.frame = CGRectMake(0, 0, 600, 350);
self.view.superview.frame = CGRectMake(100, 200, 600, 350);
}
}
but when rotate screen, the view don't be resized and width and height of superview has unexpect value.
please help my problem.

try to debug it and see what orientation you get in fact.
Did you try like this?
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if ( UIInterfaceOrientationIsPortrait(fromInterfaceOrientation) )
{
self.view.frame = CGRectMake(0, 0, 350, 600);
self.view.superview.frame = CGRectMake(200, 100, 940, 628);
}
else
{
self.view.frame = CGRectMake(0, 0, 600, 350);
self.view.superview.frame = CGRectMake(100, 200, 600, 350);
}
}
Also note that there are two portairt orientations..

Related

UIScrollview isn't displayed

Hi I'm testing out drawing and created a simple scrollview and embedded a subview without using interface builder. It displays correctly when I position everything at the origin but if I move everything down past y = 40 it disappears. Alternatively, the scrollview and subview independently display fine at whatever position I place them ie. without setting the view as a subview. Can anyone explain why this would happen?
thanks
the view to be embedded:
#implementation BLRView
- (id) initWithFrame:(CGRect) frame
{
self = [super initWithFrame:frame];
if(self)
{
[self setBackgroundColor:[UIColor blackColor]];
}
return self;
}
- (void) drawRect:(CGRect)dirtyRect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];
CGContextSetLineWidth(ctx, 30);
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
float dashphase = 1.0;
float dashlengths[] = {1.0, 20.0};
CGContextSetLineDash(ctx, dashphase , dashlengths, 2);
CGContextMoveToPoint(ctx, bounds.origin.x, bounds.origin.y);
CGContextAddLineToPoint(ctx, bounds.size.width , bounds.origin.y);
CGContextStrokePath(ctx);
}
#end
and in the appdelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CGRect screenRect = [[self window] bounds];
//this displays
CGRect viewFrame = CGRectMake(0, 0, screenRect.size.width * 3, 50);
CGRect scrollFrame = CGRectMake(0, 0, screenRect.size.width, 50);
//this is slightly cut off at the bottom
//CGRect viewFrame = CGRectMake(0, 30, screenRect.size.width * 3, 50);
//CGRect scrollFrame = CGRectMake(0, 30, screenRect.size.width, 50);
//this isnt shown
//CGRect viewFrame = CGRectMake(0, 100, screenRect.size.width * 3, 50);
//CGRect scrollFrame = CGRectMake(0, 100, screenRect.size.width, 50);
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:scrollFrame];
BLRView *view = [[BLRView alloc]initWithFrame:viewFrame];
[[self window] addSubview:scrollview];
[scrollview addSubview:view];
[scrollview setContentSize:viewFrame.size];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Sorry, i'm an idiot. The embedded view frame is being pushed down from the origin of the scroll frame
these should be
CGRect viewFrame = CGRectMake(0, 0, screenRect.size.width * 3, 50);
CGRect scrollFrame = CGRectMake(0, 30, screenRect.size.width, 50);

How to initiate revmob banner ad with exact frame and placement ID?

I tried using the suggested code from RevMob to show a banner ad with a specific placement ID without success, tried this code:
RevMobAds *revmob = [RevMobAds revMobAds];
RevMobBanner *banner = [revmob bannerWithPlacementId:#"ID_FROM_REV_MOB"];
[banner showAd];
Tried even to add the following statement
if (IS_iPad) {
banner.frame = CGRectMake(0, 958, 768, 66);
} else if (IS_WIDESCREEN){
banner.frame = CGRectMake(0, 518, 320, 50);
} else {
banner.frame = CGRectMake(0, 430, 320, 50);
}
but no success, the only way I could be able to show banner ads was this:
[RevMobAds showBannerAdWithFrame:CGRectMake(0, 958, 768, 66) withDelegate:self];
But it didn't help to add the placement ID.
I think you need to do it as the following by using the RevMobBannwerView instead:
RevMobAds *revmob = [RevMobAds revMobAds];
RevMobBannerView *revBannerView = [revmob bannerViewWithPlacementId:#"ID_FROM_REV_MOB"];
if (IS_iPad) {
revBannerView.frame = CGRectMake(0, 958, 768, 66);
} else if (IS_WIDESCREEN){
revBannerView.frame = CGRectMake(0, 518, 320, 50);
} else {
revBannerView.frame = CGRectMake(0, 430, 320, 50);
}
[revBannerView loadAd];
[self.view addSubview:revBannerView];
[self.view bringSubviewToFront:revBannerView];
When I'm adding it(RevMob version 5.9) in my project. I do it like this:
[RevMobAds startSessionWithAppID:#"my id"];
RevMobBannerView *ad = [[RevMobAds session] bannerView]; // you must retain this object
[ad loadWithSuccessHandler:^(RevMobBannerView *banner) {
banner.frame = CGRectMake(0, 381, 320, 50);
[self.window.rootViewController.view addSubview:banner];
NSLog(#"Ad loaded");
} andLoadFailHandler:^(RevMobBannerView *banner, NSError *error) {
NSLog(#"Ad error: %#",error);
} onClickHandler:^(RevMobBannerView *banner) {
NSLog(#"Ad clicked");
}];

iPhone Orientation change frame of view

I want to set position of UILable programatically depend on orientation. But When I get back to Portrait, it doesn't work.
Here is my code :
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)
{
lblUserName.frame = CGRectMake(20, 200, 280, 44);
lblPassword.frame = CGRectMake(20, 246, 280, 44);
}
else
{
lblUserName.frame = CGRectMake(20, 220, 280, 44);
lblPassword.frame = CGRectMake(20, 266, 280, 44);
}
}
There are 2 possibilities.Either you can set the label programatically.And check the condition for orientation in the - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{}
method.Otherwise you can use auto resizing mask.
try with this IF condition:
if ( UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
}
else {}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//write code here
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//write code here
}
Use this two methods for set your label when rotate device.

What is difference between self.view.frame and self.superview.frame and how to use them?

I would like to know that what is difference between these both lines of code?
self.view.frame = CGRectMake(0, 0, 320, 480);
self.view.superview.frame = CGRectMake(0, 0, 800, 900);
and I want to change the view frame when my orientation will change, because it will change the position of labels, and I want them in middle of screen, can anyone guide me ?
I am using following delegate method, for orientation, but it is not working with
self.view.frame
but it is working ok with following line
self.view.superview.frame
See the following code
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(#"LEFT");
self.view.frame = CGRectMake(100, 0, 480, 320);
NSLog(#"Show self.view.frame: %#", NSStringFromCGRect(self.view.frame));
// self.view.superview.frame = CGRectMake(-50, -70, 800, 900);
[button setFrame:CGRectMake(340, 320, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSLog(#"RIGHT");
self.view.frame = CGRectMake(0, 0, 320, 480);
NSLog(#"Show self.view.frame: %#", NSStringFromCGRect(self.view.frame));
//self.view.superview.frame = CGRectMake(10, 90, 800, 900); //It is working if I will uncomment it
[button setFrame:CGRectMake(250, 300, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view.frame = CGRectMake(0, 0, 320, 480);
//self.view.superview.frame = CGRectMake(0, 0, 800, 900);//It is working if I will uncomment it
[button setFrame:CGRectMake(250, 400, 100, 30)];
}
return YES;
}
self.view is the view of self (if we are talking about viewControllers).
self.view.superview is the view that is holding self.view.
So, in short, if you add a view to the window the superview of that view will be the window.
Setting the frame will fail if the autoresize mask isn't set correctly.
As a generic statement, the first line is trying set the frame of the current viewController's view that this code is written in.
The second line is trying to set the frame of the parent view of the view of the current viewController's view.
What this exactly means, and which one you should be using I'm afraid depends on the view hierarchy you have set up in your application.

Autoresize UIScrollView

I made a UIViewController, which programatically generates a UIScrollView. Everything's fine, but when I rotate the Device, the UIScollView should resize so it takes the complete width of my View.
Is there a way to do that without rebuilding the complete UIScrollView ?
Thx a lot !
Sebastian
This is called in my viewDidLoad:
-(void)buildmyScroller {
myScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 800, 768, 100)];
//...adding some subviews to myScroller
thumbScroller.contentSize = CGSizeMake(3000, 100);
[[self view] addSubview:myScroller];
}
Then I tried to resize myScroller with this, when I used setFrame, I said myScroller would not respond to it... :
-(void)changemyScroller {
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
[thumbScroller setFrame:CGRectMake(0, 805, 768, 150)];
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
thumbScroller.frame = CGRectMake(0, 805, 768, 150);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
thumbScroller.frame = CGRectMake(0, 549, 1024, 150);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight){
thumbScroller.frame = CGRectMake(0, 549, 1024, 150);
}
}
And called the method in didAnimateFirstHalf... cause I'm not shure where else to call it.
Thx a lot again !!
[scrollView setFrame:CGRectmake(x, y, width, height)];
//Maybe you need to do the same for the content of the scrollView to make it fit your layout
should do it. You can wrap that in an UIAnimation block if it need to be a transition.
Try this:
if(self.rowNumber == 0){
/******************* Scroller Setup *****************/
// how many pages
int pageCount = 5;
//set up the scrollView
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 960)];
// support for Landscape Orienation
if(UIInterfaceOrientationLandscapeLeft){
[scroller setFrame:CGRectMake(0,0,1024, 704)];
}
if(UIInterfaceOrientationLandscapeRight){
[scroller setFrame:CGRectMake(0,0,1024, 704)];
}