Vis*_*ngh 4 objective-c uiscrollview uiinterfaceorientation ios contentsize
我有一个分页的滚动视图.在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)];
}
Run Code Online (Sandbox Code Playgroud)
一切正常滚动视图滚动顺畅,没有对角线滚动.
但是当方向改变时
我必须再次设置scrollview的框架和内容,我将其设置如下
-(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)];
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我必须在横向模式下将内容大小的高度设置为600,这太不够了.并且它增加了另一个问题,即scrollview开始对角线滚动,我不想要,因为它看起来很奇怪.任何人都可以帮我理解我错过的地方和地点吗?
我已将scrollview的自动调整遮罩设置为
[scroll setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight];
Run Code Online (Sandbox Code Playgroud)
但改变它没有帮助.
不要用UIDeviceOrientation.请UIInterfaceOrientation改用.DeviceOrientation这里有两个你不需要的额外选项.(UIDeviceOrientationFaceUp和UIDeviceOrientationFaceDown)
Yes从... 返回shouldAutorotateToInterfaceOrientation
现在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);
}
Run Code Online (Sandbox Code Playgroud)
让,
pageNumber = 2
statusBarHeight = 20
| 归档时间: |
|
| 查看次数: |
11553 次 |
| 最近记录: |