view.frame is not working in ipad? - iphone

I am developing iPad application in landscape mode.I have set in all view controller as return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);.
but when I give change view frame through following code,it is not changing , it occupies full screen of ipad.How can i overcome this problem , any help please?I tried view.frame also.
-(IBAction)category_Click:(id)sender
{
CGRect rect = _categoryController.view.bounds;
rect.origin.x = 0;
rect.origin.y = rect.origin.y;
rect.size.width = 1024;
rect.size.height = 650;
_categoryController.view.bounds = rect;
_categoryController.view.bounds = CGRectMake(rect.origin.x, rect.origin.y,1024, 650);
[self presentModalViewController:_categoryController animated:YES];
}

Have you tried adjusting the frame after the viewController is presented? I believe that the presentModalViewController:animated: method may sometimes alter the frame during its execution. You may even need to wait until the animation is finished.
If it possible, perhaps try adjusting the view's frame in the '-(void)viewDidAppear:' method of the _categoryController's class.
Generally, though, I have found that changing the appearance of a modal view controller in a smooth way is difficult.

Check modalPresentationStyle property
_categoryController.modalPresentationStyle = UIModalPresentationFormSheet;
_categoryController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:_categoryController animated:YES];
_categoryController.view.superview.frame = CGRectMake(0, 0, 818, 739);
_categoryController.view.superview.center = self.view.center;

Related

Objective C: Subview not clickable

I'm about to implement a Facebook-like side navigation for an iPhone App. I can slide it in and out without problems but when I want to click a button or anything in that navigation view nothing happens.
Here is the code for my side navigation view I initialize in viewDidLoad:
self.sideNavigationView = [[UIView alloc] initWithFrame:CGRectMake(self.navigationController.view.frame.size.width, 20, 238, self.navigationController.view.frame.size.height-20)];
[self.navigationController.view addSubview:self.sideNavigationView];
Then I have the following method to slide it in:
- (IBAction) openMenuBar: (id) sender {
if (!self.sideNavigationIsExpanded) {
self.sideNavigationView.frame = CGRectMake(self.navigationController.view.frame.size.width, 20, 238, self.navigationController.view.frame.size.height-20);
}
CGRect destination = self.navigationController.view.frame;
CGRect sideNavigationDestination = self.sideNavigationView.frame;
sideNavigationDestination.size.width = 238;
// Slide back
if (self.sideNavigationIsExpanded) {
sideNavigationDestination.origin.x = self.view.frame.size.width;
destination.origin.x = 0;
sideNavigationDestination.origin.y = 20;
self.sideNavigationIsExpanded = NO;
}
// Slide in
else {
sideNavigationDestination.origin.x = self.view.frame.size.width;
destination.origin.x = - sideNavigationDestination.size.width;
sideNavigationDestination.origin.y = 20;
self.sideNavigationIsExpanded = YES;
self.sideNavigationView.hidden = NO;
}
[UIView animateWithDuration:0.25 animations:^{
self.sideNavigationView.frame = sideNavigationDestination;
self.navigationController.view.frame = destination;
} completion:^(BOOL finished) {
if (!self.sideNavigationIsExpanded)
self.sideNavigationView.hidden = YES;
self.view.userInteractionEnabled = !self.sideNavigationIsExpanded;
self.sideNavigationView.userInteractionEnabled = self.sideNavigationIsExpanded;
}];
}
I tried several combinations of userInteractionEnabled = YES at every subview... Changed nothing...
Could it be that the subviews of my sideNavigationView aren't selectable because when I initialize it it is out of sight? I tried initializing it with a frame in sight (CGRectMake(0,0,238,300)) and that worked. -_-
So how could I solve my problem?
As sideNavigationView is a subview of self.view then this line:
self.view.userInteractionEnabled = !self.sideNavigationIsExpanded;
is preventing touches from reaching your sideNavigationView at all. A view will not pass a touch on to its subviews if its own userInteractionEnabled property is set to NO, even though you have set the subview's property to YES in the next line.
As for initialising off-screen, I don't believe this will have any bearing on whether the view can receive touches once it is moved back onscreen.

iPhone: How to set UIViewController frame?

I have a root UIViewController that I am adding other UIViewController's as subviews. Currently each of the subviews are too low down (covering up my custom build tabbar). When I try to so something like the following, it does not work:
// Test setting frame size to see if it works
self.view.frame = CGRectMake(0, 0, 200, 200);
That does nothing to change the frame size.
So, my question is, how can I set my frame when the UIViewController is initialized after it is added as the subview?
#Nic i think when you are adding that other view, at that time you should define the other views frame size like this:
Someviewcontroller *c = initWithNibName
c.view.frame = CGRectMake(0, 0, 200, 200);
[self addSubView:c];
i dont know if this will work but it is something like this.
First, ensure that your frame is actually not changing size. Likely it /is/ changing size, but you are expecting it to clip its contents; this behavior is not enabled by default on a UIView, and would need to be set via:
[[self view] setClipsToBounds:YES];
To double check and ensure that your frame is / is not changing size after setting the new frame, try logging this:
NSLog(#"New frame is: %#", NSStringFromCGRect([[self view] frame]));
or simply setting a breakpoint after the change and inspecting the value in your debugger.
Then there's always...
// Screen, less StatusBar
CGRect l_RectFrame = [UIScreen mainScreen].applicationFrame;
OR...
// Entire Screen
CGRect l_RectFrame = [UIScreen mainScreen].bounds;
MyView *l_aView = [[MyView alloc] initWithFrame:l_RectFrame];
...

iAd left white blank screen after closed

I got a problem to integrate iAd in my iPhone apps -- the banner ad is fine when it expends (see http://www.clingmarks.com/iAd1.png and http://www.clingmarks.com/iAd2.png), however, when I close it, it left a white blank screen (see http://www.clingmarks.com/iAd3.png). I couldn't figure out why. Here is how I integrate the ad:
Because I need to support other ads for lower version of iPhone OSes, I add a container view at the top of the apps, whose view controller is AdViewController. When the view is loaded, I create a AdBannerView programmatically and add it as a subview to the AdViewController.view. Here is the code in the viewDidLoad method:
Class adClass = (NSClassFromString(#"ADBannerView"));
if (adClass != nil) {
iAdView = [[ADBannerView alloc] initWithFrame:CGRectZero];
iAdView.frame = CGRectOffset(iAdView.frame, 0, -50);
iAdView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
iAdView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
iAdView.delegate = self;
iadViewIsVisible = NO;
[self.view addSubview:iAdView];
} else {
// init google adsense
}
Following are the delegate methods:
enter code here
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (!iadViewIsVisible) {
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// banner is invisible now and moved out of the screen on 50 px
banner.frame = CGRectOffset(banner.frame, 0, 50);
[UIView commitAnimations];
iadViewIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
if (iadViewIsVisible) {
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// banner is visible and we move it out of the screen, due to connection issue
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
iadViewIsVisible = NO;
}
}
Eventually I figured it out myself. It turns out the ADBannerView's parent view must be a fullscreen view. I my case above, I added AdBannerView to my adView, which is a view with size 320x50. When I changed its parent view to a fullscreen view, everything works. I am not sure if this is a bug in iAd, but certainly something tricky.
When the banner finishes, it moves itself to the top of the screen even if that means having a negative y coordinate. I center the banner when it finishes. In my case there is a view controller for just the banner, so it is only full screen when the ad is clicked.
-(void) bannerViewActionDidFinish:(UIView *)inBanner {
CGRect frame = [inBanner frame];
frame.origin.x = frame.size.width * 0.5;
frame.origin.y = frame.size.height * 0.5;
[inBanner setCenter:frame.origin];
}
Hey David! I know what you mean, I'm also using an own AdvertisementViewController which calls different ad networks.
So iAd is not in a full screen view but inside a 320x50 view.
Simply do this:
-(void) bannerViewActionDidFinish:(ADBannerView *)inBanner {
[self.view setFrame:CGRectMake(0.0f, 0.0f, 320.0f, 50.0f)];
}
So the outer view container (self.view) is resized to its original size. iAd is resizing it to fullscreen for displaying the ad when an iAd is shown.

Dynamically changing position of a UIView

Here's my setup. I have a viewcontroller that I'm creating and adding as a subview. The viewcontroller presents some options that a user can chose from. The viewcontroller is being pushed in response to a "long press" gesture. Within the viewcontroller, I added a child UIView to group some other controls together so I can move them around the screen as a unit and, when they are displayed, center them on the location of the long press. Here is the code that instantiates the view controller, changes its location, and adds it as a subview:
UserOptions *opts = [[UserOptions alloc] initWithNibName:#"UserOptions" bundle:nil];
[opts recenterOptions:location];
[self.view addSubview:opts.view];
That bit of code does create and push the viewcontroller, but the call to recenterOptions doesn't do anything. Here is that method:
- (void) recenterOptions:(CGPoint)location {
CGRect oldFrame = self.optionsView.frame;
CGFloat newX = location.x; // + oldFrame.size.width / 2.0;
CGFloat newY = location.y; // + oldFrame.size.height / 2.0;
CGRect newFrame = CGRectMake(newX, newY, oldFrame.size.width, oldFrame.size.height);
self.optionsView.frame = newFrame;
}
Note that self.optionsView is the child UIView that I added to the viewcontroller's nib.
Does anyone know why I'm unable to change the location of the UIView?
Regards,
Eric
A couple things. First, try adding the view to the view hierarchy before calling -recenterOptions:
UserOptions *opts = [[UserOptions alloc] initWithNibName:#"UserOptions"
bundle:nil];
[self.view addSubview:opts.view];
[opts recenterOptions:location];
Next, just set the center of the view instead of trying to change its frame:
- (void) recenterOptions:(CGPoint)location {
[[self optionsView] setCenter:location];
}
Are you verifying that your optionsView is valid (non-nil)?
I agree with Rob, btw. You need to change your title to match your question.
Views are loaded lazily. Until you call self.view, the view is not loaded and optionsView is nil, hence self.optionsView.frame = newFrame does nothing.

UIPopoverController animates when keyboard pops up

Heey,
In my iPad application I have a UIPopoverController with a UIViewController containing some textfields.
When the keyboard comes up, the Popover gets animated to fit. Does anybody know how to disable this?
Thanks
I dont think you can except make the popovers smaller in height...Its done like that so when your keyboard pops up none of the popover gets covered by it (thus it shrinks back), however i have found it to be annoying at times since it messed with table views (not making them able to scroll all the way and having to resize them)
It would be great if it did actually animate to better part of the screen, I think you mean it actually shrinks the popUp which is mostly not good.(which i see during rotation in my case). You can not keep the popUp from squishing, UNLESS you move a view. The best way to handle this is to temporarily move your entire main UIView of the whole screen up with the keyBoard by the difference between the size of your pop up, and how much that pop up would shrink if you did not move it up... you can't just move it up by the size of your keyboard, because popUps up high would then also be effected. This code below is for when the keyboard rotated, similar code for when it is first introduced. easy to do, EXCept for when you rotate the screen, then things get tricky...
in otherwords, sometimes your UIView will not move at all, sometimes it will move up by a good 170 points.
//-----------------finding the keyboard top (also used on "didRotate")----very handy,
// took me hours to figure out an absolute always work topOfKeyboard, so here you go.-------
//--------------------------------------------------------------------------------
- (void)keyboardWillShow:(NSNotification*)notification
{
NSLog(#" keyboardWillShow");
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
NSDictionary* info = [notification userInfo];
keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardRect = [(UIView*)keyWindow convertRect:keyboardRect toView:mainViewController.view];
keyboardIsVisible = TRUE;
topOfKeyboard = keyboardRect.origin.y;
the tricky part is finding the PopUp bottom, because there appears to be code in the popup itself or the convertRect:toView: code that makes the origin flaky , (if you try to "view" origin after a "convertRect:toView:" code), it wants to move and be in different spots during rotation,(or one of it's super views) so bottom calc comes out different some times,(not predicable) because of async process of the rotation of different elements possibly because the popUp itself has many superviews down in the pop up. (to see problem in action with pop up and, must have keyboard effecting popup, move the whole view up then log the "origin" and "size" of popUp after the "convertRect:toView:" code)... the "origin" i'm talking about is the origin of the frame after it is translated to the main view (or atleast a couple views up) with the "convertRect:toView:" code....
update:(it appears if you move down about 3 superviews from the popUp, the flakiness goes away... (this code below was in a "didRotate" ipad type of code. That is that the popUp has several superviews before you can get to the one that is projected ontop of the proper frame
UIPopoverController probably should have a property that has the origin that is predicted after a rotation in it, in a type of "will rotate" kind of code, (because of the keyboard problem), instead of just the "popoverContentSize", (also should include the popoverContentSize that would have been without the keyboard as another variable, ".. anyway This is how I had to do it, see code below.
//--------------------------------------------------------------------------------
- (void) checkRotation
{
NSLog(#" ");
NSLog(#"checkRotation");
if (wasOffset)
{
wasOffset = false;
[UIImageView beginAnimations:nil context:NULL];
[UIImageView setAnimationDuration:0.2f];
CGRect frame = carousel.frame;
frame.origin.y += offset;
carousel.frame = frame;
[UIImageView commitAnimations];
[popPickerController presentPopoverFromRect:zoneButton.frame inView:[zoneButton superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
if (popPickerController.popoverVisible)
{
if (keyboardIsVisible)
{
wasOffset = false;
[popPickerController presentPopoverFromRect:zoneButton.frame inView:[zoneButton superview]
permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
upView3 = [[[popPickerController.contentViewController.view superview] superview] superview]; //hey it works... :o)
//NSLog(#" ");
//NSLog(#"upView3.frame.origin.x = %f",upView3.frame.origin.x);
//NSLog(#"upView3.frame.origin.y = %f",upView3.frame.origin.y);
//NSLog(#"upView3.frame.size.height = %f",upView3.frame.size.height);
//NSLog(#"upView3.frame.size.width = %f",upView3.frame.size.width);
//NSLog(#" ");
popUpRect.origin.x = upView3.frame.origin.x;
popUpRect.origin.y = upView3.frame.origin.y;
popUpRect.size.height = popUpSize.height;
popUpRect.size.width = popUpSize.width; //you must save the size because the keyboard destroys it before you can use it. very tricky....
//NSLog(#" ");
//NSLog(#"popUpRect.origin.x = %f",popUpRect.origin.x);
//NSLog(#"popUpRect.origin.y = %f",popUpRect.origin.y);
//NSLog(#"popUpRect.size.height = %f",popUpRect.size.height);
//NSLog(#"popUpRect.size.width = %f",popUpRect.size.width);
//NSLog(#" ");
//NSLog(#" ");
//NSLog(#"keyboardIsVisible = %d", keyboardIsVisible);
//NSLog(#" ");
//NSLog(#"keyboardRect.origin.x = %f",keyboardRect.origin.x);
//NSLog(#"keyboardRect.origin.y = %f",keyboardRect.origin.y);
//NSLog(#"keyboardRect.size.height = %f",keyboardRect.size.height);
//NSLog(#"keyboardRect.size.width = %f",keyboardRect.size.width);
//NSLog(#"topOfKeyboard = %f",topOfKeyboard);
CGFloat bottomOfPicker = popUpRect.origin.y + popUpRect.size.height - amountShadowCanEncroach;
//NSLog(#" ");
//NSLog(#"bottomOfPicker = %f",bottomOfPicker);
//NSLog(#"topOfKeyboard = %f",topOfKeyboard);
//NSLog(#" ");
if (bottomOfPicker > topOfKeyboard)
{
wasOffset = true;
offset = bottomOfPicker - topOfKeyboard;
NSLog(#"offset = %f",offset);
[UIImageView beginAnimations:nil context:NULL];
[UIImageView setAnimationDuration:0.2f];
CGRect frame = carousel.frame;
frame.origin.y -= offset;
carousel.frame = frame;
[UIImageView commitAnimations];
}
}
[popPickerController presentPopoverFromRect:zoneButton.frame inView:[zoneButton superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
and moving the main UIView back
//-----------------------------------------------------------------------------
- (void) searchDidEndEditing
{
NSLog(#"searchDidEndEditing");
keyboardIsVisible = false;
if (wasOffset)
{
wasOffset = false;
[UIImageView beginAnimations:nil context:NULL];
[UIImageView setAnimationDuration:0.2f];
CGRect frame = mainView.frame;
frame.origin.y += offset;
mainView.frame = frame;
[UIImageView commitAnimations];
if (zoneButton.selected)
[popPickerController presentPopoverFromRect:zoneButton.frame inView:[zoneButton superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}