How to center alignment all the contents of view. When i am rotating iPad contents are not align center it self how to resolve this problem??
Thanks!!!
Provide an implementation of the didRotateFromInterfaceOrientation method and center all subviews.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
for(UIView *subview in [self.view subviews]) {
subview.center = self.view.center;
}
}
You should change programmatic-ally the contents' frame for both Landscape and portrait modes.
You can try this as:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
// set contents' frame for landscape
}
else {
// set contents' frame for portrait
}
return YES;
}
Related
Here is my code to set at the center
UIButton *videoImage = [[UIButton alloc]initWithFrame:CGRectMake((secondImageView.frame.size.width/2)-50,(secondImageView.frame.size.height/2)-50,50,50)];
videoImage.transform = CGAffineTransformMakeRotation(ang*(3.14/180));
[videoImage setBackgroundImage:[UIImage imageNamed:#"PlayButton.png"] forState:UIControlStateNormal];
[videoImage addTarget:self action:#selector(PlayMusicOnClickofButton:) forControlEvents:UIControlEventTouchUpInside];
[secondImageView addSubview:videoImage];
videoImage.tag = k+1000;
secondImageView.userInteractionEnabled = YES;
[videoImage release];
The problem that i am facing is when i change orientation of button it's position gets changed. It doesn't remain at center. Please help
Thanks
Actually you should use this method....
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
//handle how you want to show in landscape mode
}
else
{
//handle how you want to show in portrait mode
}
return YES;
}
You need to handle this. What you are doing is correct. You need to do tweak it when the orientation changes as the dimensions of your parent view change. Implement willAnimateRotationToInterfaceOrientation delegate. check this out -
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
//handle how you want to show in landscape mode
}
else
{
//handle how you want to show in portrait mode
}
}
I am working on a project where I have added UIWebviews in the UIscrollview to show the description. I did this because I want to add the swipe effect to move to the new description page. Now, I want to resize that UIscrollview and its content (i.e uiwebview) when the orientation changes (i.e portrait to landscape or Viceversa).
Please give me any sample code or any suggestions for it.
To resize the webview you have to write:-
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
webview.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * someValue1,
scrollView.frame.size.height * someValue2);
}
You can put your code in the following code block :
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if ([self isPadPortrait]) {
// Your code for Portrait
// set frame here
} else if ([self isPadLandscape]) {
// Your code for Landscape
// set frame here
}
and following code will take care of orientation change :
- (BOOL)isPadPortrait
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
&& (self.interfaceOrientation == UIInterfaceOrientationPortrait
|| self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}
- (BOOL)isPadLandscape
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
&& (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight
|| self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft));
}
You can set the autoresizingMask of scrollView and add this code to your .m file according to your requirement.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * someValue1,
scrollView.frame.size.height * someValue2);
}
Here is a link to a similar question and their solution:
Scale image to fit screen on iPhone rotation
They used a combination of AutoresizingMasks and ContentModes for both the ScrollView and, in their case, an ImageView, although I imagine the same solution would work for your WebView.
when ever the orientation changed
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
// this delegate method will called if we set should auto orientation return yes;
when ever we changed orientation so set frames..
}
for example
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
appDelegate.interface=interfaceOrientation;
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
// SET FRAMES FOR LANDSCAPE HERE
}
if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationPortrait)
{
//SET FRAMES FOR Portrait HERE
}
}
My landscape subview is being cut off by the previous view, why is it like that?
Cut off as in the previous view is at the bottom part around 1/3 quarter of the screen even when the new subview is added. Something like this > http://i41.photobucket.com/albums/e253/welzenn99/123.png
you need to set that view's frame in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
OR in
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
in the method where you tell the subview to come out ([self.view addSubview:someView.view];) add
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if ((orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)) {
//rotate to landscape
someView.view.frame = CGRectMake(0,0,480,320);
}
else if (orientation == UIDeviceOrientationPortrait) {
//rotate to portrait
isomeView.view.frame = CGRectMake(0,0,320,480);
}
worked for me. good luck
I am resizing the navigation bar when my application switches into landscape orientation.
But i am not able to set the content size of navigation bar according to its height.
Landscape image :- In this image the top left bar item and title of navigation bar are not resizing when it switches into landscape orientation....they should get re sized according to height of navigation bar.
Please suggest me any idea for it?
Thanks
Deepika
override the method:
- (UIView *)rotatingHeaderView
{
return yourNavigationBar;
}
this method will get called when your device orientation takes place.
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(mainLoginView.superview == self.view || modulesView.superview == self.view)
{
return NO;
}
else
{
return YES;
}
}
replace this method with your code and reply me what happens :)
I am doing in this delegate method:-
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
if(mainLoginView.superview == self.view || modulesView.superview == self.view){
return NO;
}else{
[self rotateTheNavigation:contactsNavigation withOrientation:interfaceOrientation];
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
If you have to recalculate your views, you'd better put your code into
-
(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration,
or -(void)
didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
shouldAutorotateToInterfaceOrientation is only supposed to respond to the question : "This view Controller should rotate or not ?".
For example :
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// Get the new screen width
self.cellWidth = self.view.frame.size.width;
// Recalculate the view
[self rebuildTheView];
}
In most of the cases, implementing shouldAutorotate in all your ViewController is sufficient if you set the right resize settings in Interface Builder. Check that you did that before implementing didRotateFromInterfaceOrientation or willAnimateRotationToInterfaceOrientation
For example :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [ViewToolkit orientationIsSupported:interfaceOrientation];
}
I have a UITableView being shown in a popover. One cell in my table view has a UITextField in it. When the text field is being edited (the keyboard is visible) and I rotate the device from portrait to landscape and then try to scroll the table view, it keeps going past its bounds, instead of stopping and "bouncing".
Does anyone know how to fix this?
You can try to detect when the device is rotated and call the tableViewController method that adjusts scrolling position of selected cell to the area you need.
You can use following code when rotate device
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if ([appDelegate.aPopover isPopoverVisible])
{
[appDelegate.aPopover dismissPopoverAnimated:NO];
CGSize size = [self rotatedSize];
size.height -= [[self.view viewWithTag:154] frame].size.height;
appDelegate.aPopover.popoverContentSize = size;
[appDelegate.aPopover presentPopoverFromRect:[[self.view viewWithTag:154] frame]
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
}
// for set popoverview size when rotate screen
-(CGSize)rotatedSize
{
UIDeviceOrientation orientation1 = [[UIDevice currentDevice] orientation];
UIInterfaceOrientation toInterfaceOrientation = orientation1;
if ((toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
{
return self.view.frame.size;
}
else
{
return CGSizeMake(self.view.frame.size.height, self.view.frame.size.width);
}
}
Thank you,