How to hide a TextView in iOS-landscape - ios

I am a newbie in iOS-programming (trying to convert my android-app to iOS).
I am using the storyboard and want to create a view, which has on the top a scrollview (with an ImageView inside) and on the bottom there is a TextView. This should be the View, if the users use the portrait-orientation.
Now I want that the ViewCOntroller in the landscape-orientation only shows the scrollview and the imageviews (that are inside the scrollview). The textview should be hidden in landscape.
Is it possible to make the images bigger, but hold the aspect ratio ( in android:adjustViewBounds) ?
How can I handle this? I've tried it by using the "autosizing", but the result wasn't good.

Apple provides a method that is called when the app will change its orientation.
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[textview setHidden:YES];
} else {
[textview setHidden:NO];
}
}
Just add it to your view controller. Check more on Apple's reference

Related

How to clear the content of subview before drawing

i use two blocks to draw some buttons on my subview dynamically. One calculates frames for Portrait mode and the other does for Landscape. It works well but when i rotate, it writes over the old ones. Hence, some of my buttons come twice. Here is my code of detecting the oriantation:
//i have defined blocks in viewDidLoad: and everything is ok till here
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
dispatch_async(dispatch_get_main_queue(), PortraitBlock);
}
else
{
dispatch_async(dispatch_get_main_queue(), LandscapeBlock);
}
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
Now, how can i clean the view i add the buttons on?
Note: I add buttons on a UIView object, and that object too is on a UIScrollView object
hi,.,
Try below code with this all your button which you alloc on the view
will be remove.
for(UIButton *view in yourview.subviews)
{
[view removeFromSuperview];
}
Before removing the views from your view first check the class and then remove it. After that you can add your buttons on the screen based on the orientation.
for(UIView *view in [View to add buttons].subviews)
{
if ([view isKindOfClass:[UIButton class]]) [view removeFromSuperview];
}
If you are using any customButton the mention your customButtonClassName in place of UIButton.
Don't add new buttons at all. Just change the old frames.
If you need to add new buttons. Just remove the old ones!? To remove all subviews, you could use:
for(UIView* view in self.view.subviews)
{
[view removeFromSuperview];
}

iOS best way to change the UI when orientation is changed

Which is the best way to change the UI when orientation is changed?
For example, use two different UIView one portrait and a landscape and show one of both if orientation is changed, or use one UIView and change the UI control sizes and positions?
Any other ideas?
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
NSLog(#"Change to custom UI for landscape");
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
NSLog(#"Change to custom UI for portrait");
}
}
I always recommend using the autoresizingmask for all the subviews in the view controller view. With this being set correctly all the views will resize automatically from the orientation and you don't need the extra rotation specific subviews (one portrait view and one landscape view).

Cannot resize AdMob's view upon orientation change

What I tried so far is, in viewDidLoad, I called
self.bannerView.autoresizingMask=UIViewAutoresizingFlexibleWidth;
and
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)newInterfaceOrientation duration:(NSTimeInterval)duration {
if (newInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || newInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
self.bannerView.frame=CGRectMake(0.0,
0.0,
480.0,
GAD_SIZE_320x50.height);
}
// Position the UI elements for portrait mode
else {
self.bannerView.frame=CGRectMake(0.0,
0.0,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height);
}
}
Both of these didn't work for me.
Hmm, I don't think that AdMob's creatives can stretch to fit the size of the screen when in landscape. So despite the fact that you're stretching the frame of the view to fit, the ad itself I think will stay the same size.
This means you should still see an ad come in on orientation changes, it will just look like it's the same size (make sure to make another request for an ad in the willAnimateRotationToInterfaceOrientation: method to see this).
You don't need to do any moves, but you must set correct rootViewController for adMovView.
If you use view controller model please add line in each custom view controller
adMobView.rootViewController = viewController;
where viewController - root view controller of your app.
Do not code like this
adMobView.rootViewController = self;
in custom view!

UITextField Custom Keyboard and Response to Device Orientation

I'm trying to attach a custom keyboard to a UITextField, and the keyboard would respond to different view orientations (landscape, portrait, etc). In short, the issue is that while the keyboard does show up, when responding to view orientation when I rotate the device, the dimensions of the keyboard is all messed up, and I'm not quite sure how to set the keyboard frame/bounds correctly. Hope someone here can give me a hand!
Here is my design:
The keyboard is built from a subclass of UIViewController (from xib). There are two views: landscapeKeyboardView and portraitKeyboardView.
The keyboard is attached to the UITextField through the property "inputView".
The rotation of the keyboard view is done in the function:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientationduration:(NSTimeInterval)duration
{
if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight))
{
self.view = self.landscapeKeyboardView;
}else if((toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
{
self.view = self.portraitKeyboardView;
}
}
The rotation does occur, but the view dimension is all messed up after the rotation. I have also tried to manually set the frame or the bounds of the view of either self.view or landscapeKeyboardView or portraitKeyboardView, and none of them seemed to solve the problem.
Does anyone know how to work around this issue? Or is there a much better design pattern for custom keyboards?
Thanks!
You probably need to translate the CGRect for rotation. UIView has a number of instance methods for converting rects and points. It accounts for views outside the frame of another view (to find out how far below a UIPopover the keyboard rect is, for example) as well as rotation. convertRect:fromView: is just one example, but they all start with 'convert'.

How to load different XIBs for different device orientations for same viewcontroller?

The documentation says that if I want to support both portrait and landscape, I basically have two ways of doing that:
Set up the viewcontroller's view so that the subviews autoresize correctly and make smaller changes programmatically at runtime
If the changes are more substantial, create an alternative landscape interface and push/pop the alternative modal viewcontroller at runtime
I would like to present the info where the layout is substantially different, but logic is the same. Ideally, I would load another XIB for the same viewcontroller, but it does not seem to be an option.
Sounds like #2 is what I need to do, but my problem with that is that it sounds like it would use the standard modalviewcontroller animations that are nothing like the device rotation animation. (Of course, being the lazywebber that I am, I did not test this hypothesis.)
So, how do I load an alternative layout for landscape with the same viewcontroller but different XIB? Should I use the method #2 above and is the rotation animation natural? Or is there some other way?
I instantiate my UIView instances in -viewDidLoad: and add them as subviews to the view controller's view property:
- (void) viewDidLoad {
[super viewDidLoad];
self.myView = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 280.0f, 210.0f)] autorelease];
// ...
[self.view addSubview:myView];
}
I then call -viewWillAppear: to center those subviews:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self adjustViewsForOrientation:[[UIDevice currentDevice] orientation]];
}
I also override -willRotateToInterfaceOrientation:duration:
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)newInterfaceOrientation duration:(NSTimeInterval)duration {
[self adjustViewsForOrientation:newInterfaceOrientation];
}
The -adjustViewsForOrientation: method sets the center CGPoint of various subview objects, depending on the device's orientation:
- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
myView.center = CGPointMake(235.0f, 42.0f);
// ...
}
else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
myView.center = CGPointMake(160.0f, 52.0f);
// ...
}
}
When the view controller is loaded, the UIView instances are created and positioned based on the device's current orientation. If the device is subsequently rotated, the views are re-centered to new coordinates.
To make this smoother, one could probably use a keyed animation in -adjustViewsForOrientation:, so that the subviews more gracefully move from one center to the other. But the above works for me, for now.

Resources