UIScrollview方向更改时的内容大小
我有一个分页滚动查看。 在viewDidLoad我检查,如果当前的方向是风景,然后我设置其内容的高度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)]; }
一切工作正常滚动视图滚动光滑,没有对angular线滚动。
但是当方向改变时,
我必须设置滚动视图的框架和内容再次,我设置如下
-(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)]; } }
我不明白为什么我必须在横向模式下将内容大小的高度设置为600,而这还不够。 它增加了一个问题,滚动视图开始对angular线滚动,我不想要,因为它看起来很奇怪。 任何人都可以帮助我了解我错过了什么和什么?
我已经设置scrollview的自动调整掩码为
[scroll setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight];
但改变它没有帮助。
这是你的代码中的一个问题。 你为什么要设置这样的框架大小? 你只有320px width
的屏幕尺寸。 而当它变成landscape
,高度将只有320px
。 但是,您将滚动height
设置为480px
并且它goes out of the screen
并start to scroll diagonally
。
self.scroll.frame = CGRectMake(0,0,480,480);
而不是那个帧大小,像这样改变
self.scroll.frame = CGRectMake(0,0,480,320);
而且您需要根据您在滚动视图中的内容设置内容大小 。
-
不要使用
UIDeviceOrientation
。 改用UIInterfaceOrientation
。DeviceOrientation
有两个额外的选项,你不需要在这里。 (UIDeviceOrientationFaceUp
和UIDeviceOrientationFaceDown
) -
从
shouldAutorotateToInterfaceOrientation
返回Yes
-
现在
willRotateToInterfaceOrientation: duration:
将在您每次旋转设备时调用。 -
像这样实现这个方法。
-(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); }
让,
pageNumber = 2
statusBarHeight = 20