UIScrollview content size when orientation changes - objective-c

I have a scrollview with pagination. In viewDidLoad i check if the current orientation is landscape then i set its contentsize's height 440
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages,340)];
}
else if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]))
{
[scroll setFrame:CGRectMake(0,0,480,480)];
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 440)];
}
everything works fine scrollview scrolls smoothy and there is no diagonal scrolling.
but when orientation changes,
i have to set scrollview's frame and contentsize again and i am setting it as follow
-(void)orientationChanged:(id)object
{
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
self.scroll.frame = [[UIScreen mainScreen]bounds];
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 340)];
}
else
{
self.scroll.frame = CGRectMake(0,0,480,480);
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 600)];
}
}
i cant understand why i have to set content size's height upto 600 in landscape mode, and that too isnt enough. and it adds one more problem that scrollview starts scrolling diagonally which i dont want as it looks so weird. Can anyone help me understanding where and what i am missing?
i have set scrollview's autoresizing mask as
[scroll setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight];
but changing it doesnt help.

Don't use UIDeviceOrientation. Use UIInterfaceOrientation instead. DeviceOrientation has two extra options you don't need here. (UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown)
Return Yes from shouldAutorotateToInterfaceOrientation
Now willRotateToInterfaceOrientation: duration: will be called every time you rotate your device.
Implement this method like this.
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
CGRect frame;
int pageNumber = 2;
int statusBarHeight = 20;
if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
frame = CGRectMake(0, 0, 480, 320 - statusBarHeight);
} else {
frame = CGRectMake(0, 0, 320, 480 - statusBarHeight);
}
scrollView.frame = frame;
scrollView.contentSize = CGSizeMake(frame.size.width * 2, frame.size.height);
}
Let,
pageNumber = 2
statusBarHeight = 20

Here it is a problem in your code. Why you are setting the frame size like this?? You have only the screen size of 320px width. And when it changes to landscape, height will be only 320px. But you are setting the scroll height as 480px and it goes out of the screen and start to scroll diagonally.
self.scroll.frame = CGRectMake(0,0,480,480);
Instead of that frame size, change like this
self.scroll.frame = CGRectMake(0,0,480,320);
And you need to set the content size depend on the content you are having inside the scroll view in either orientation.

Related

Resizing the height of tabelview according to the content size so that it only scroll when it doesn't fit into the view?

I already checked all the answer related to this. My code only works if I disable or enabling scrolling from storyboard not from my code.
It always shows the content size height and tableview height as same. I am not using Autolayout.
[tableView reloadData];
tableView.frame.size.height = tableView.contentSize.height;
Try this
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self adjustHeightOfTableview];
}
write adjustHeightOfTableview this method as
- (void)adjustHeightOfTableview
{
CGFloat height = self.tableView.contentSize.height;
CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;
// if the height of the content is greater than the maxHeight of
// total space on the screen, limit the height to the size of the
// superview.
if (height > maxHeight)
height = maxHeight;
// now set the frame accordingly
[UIView animateWithDuration:0.25 animations:^{
CGRect frame = self.tableView.frame;
frame.size.height = height;
self.tableView.frame = frame;
// if you have other controls that should be resized/moved to accommodate
// the resized tableview, do that here, too
}];
}
Hope it helps.

How to scale a UIWebview properly

I have a UIWebView that I want to resize using a "scale" factor, lets say, 0.65. So what I want to do is multiply both width and height of the webview ( that, in the beginning, is equal to the screen width and height) for this scale (0.65), so it will become smaller than the screen. Is there anyway to do that and make sure that the webview will maintain the proportions when I rotate it to landscape?
Here's what I am doing rigth now. It works fine when I am only in portrait mode, but it turn into a huge mess when I turn it to landscape mode.
webview.delegate = self;
webview.frame = CGRectMake(0, 0, _adView.frame.size.width * _scale, _adView.frame.size.height * _scale);
webview.center = CGPointMake(_adView.frame.size.width/2, _adView.frame.size.height/2);
webview.bounds = webview.frame;
webview.opaque = NO;
webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webview.scalesPageToFit = YES;
Have you tried updating your frame during orientation change?
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Update your views here
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

Center UIPageControl after orientation change

I'm trying to centralize a UIPageControl in portrait and landscape modes, but it isn't working, the x changes after device rotation.
#interface DetailViewController : UIViewController<UIScrollViewDelegate>
{
UIPageControl *pageControl;
}
- (void)viewDidLoad
{
pageControl = [[UIPageControl alloc] init] ;
[self renderMyView];
[self.view addSubview:pageControl];
}
- (void)renderMyView
{
if(isPortrait)
{
pageControl.frame = CGRectMake(200, 976, 0, 0);
} else {
pageControl.frame = CGRectMake(200, 720, 0, 0);
}
}
The renderMyView is executed on didLoad and didRotate.
At first time viewDidLoad works well in portrait and landscape, but if I rotate the device the pageControl appears in a different x != 200.
I've also tried pageControl.center, but it didn't work.
How can I keep it centralized?
1) In viewDidLoad call renderMyView.
2) After device has rotated don't call method renderMyView
3) Replace your renderMyView with this one:
- (void)renderMyView
{
if(isPortrait)
{
pageControl.frame = CGRectMake(200, 976, 0, 0);
} else {
pageControl.frame = CGRectMake(200, 720, 0, 0);
}
pageControl.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
}
So, don't call my renderMyView more then one time. AutoresizingMask will do all you need.
Tell me, please, if it works for you.
PS: For more information about the autoresizingMask, read the UIView Documentation.
probably a very old question, but I'll share my way to center the UIPageControl. Just set the width of the UIPageControl to be the same width as the view's bounds size width. So, In portrait mode it will be 768 and in the landscape it will be 1024. It works for me.
Set the UIView autoresizingMask in code:
pageControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
Or you can also do this in interface builder.

UIView cut off in landscape mode

I have several UIViews added as subViews to my main view controller. The way I have the views laid out looks fine in portrait mode, however, when it is tilted in to landscape mode, the bottom parts of the subviews gets cut off. I figured if I set the height of the frame, it would allow the screen to scroll and thus reveal the parts of the subViews that were cut off - but it doesn't.
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[notification object] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft) {
self.view.frame = CGRectMake(0.0, 0.0, 1024.0, maxHeight);
} else if (orientation == UIDeviceOrientationLandscapeRight) {
self.view.frame = CGRectMake(0.0, 0.0, 1024.0, maxHeight);
}
}
I also tried adding a UIScrollView to the main view, then adding the afore mentioned subViews to the scrollView
- (void)viewDidLoad
{
scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
[self.view addSubview:scrollView];
//add views to scrollView
[super viewDidLoad];
}
How can I make the main view "scroll" if the sub-views exceed the screen size?
After you add your views to the scroll view, you must set the contentSize property of the scroll view so that the scroll view knows there's content to be scrolled to.

iPhone SDK UIScrollView doesn't get touch events after moving it

I'm subclassing UIScrollView and on the start I fill this ShowsScrollView with some items. After filling it, I setup frame and contentSize to this ShowsScrollView. Everything works fine for now, i get touches events, scrolling is working..
But after rotation to landscape, I change x and y coordinates of ShowsScrollView frame, to move it from bottom to top right corner. Then I resize it (change width and height of ShowsScrollView frame) and reorder items in this scroll. At the end I setup new contentSize.
Now i get touches event only on first 1/4 of scrollview, scrolling also work only on 1/4 of scrollview, but scroll all items in scrollview.
After all actions I write a log: NSLog(#"ViewController: setLandscape finished: size: %f, %f content: %f,%f",scrollView.frame.size.width,scrollView.frame.size.height, scrollView.contentSize.width, scrollView.contentSize.height );
Values are correct:
ViewController: setLandscape finished: size: 390.000000, 723.000000 content: 390.000000,950.000000
On rotating back to portrait, I move and resize all thing back and everything works fine..
Please help!
I had the same issue and managed to get it working by setting the size of self.view again AFTER changing the scrollview's frame. I don't know why this works, but it does.
// Set the view's frame
if (UIDeviceOrientationIsLandscape(toOrientation)) {
[self.view setSize:CGSizeMake(1024, 724)];
} else {
[self.view setSize:CGSizeMake(768, 960)];
}
// Set scrollview frame
if (UIInterfaceOrientationIsLandscape(toOrientation)) {
self.scrollView.frame = CGRectMake(0, 0, 100, 300);
self.scrollView.contentSize = CGSizeMake(100, 600);
}
else {
self.scrollView.frame = CGRectMake(0, 0, 300, 100);
self.scrollView.contentSize = CGSizeMake(600, 100);
}
// Move your content around here
// Set the view's frame again
if (UIDeviceOrientationIsLandscape(toOrientation)) {
[self.view setSize:CGSizeMake(1024, 724)];
} else {
[self.view setSize:CGSizeMake(768, 960)];
}
You have to bring scrollView to front of the superView, for example:
[self.view bringSubviewToFron:scrollView];