【问题标题】:Scroll view location at same point when changed from portrait to landscape and vice versa从纵向更改为横向时在同一点滚动视图位置,反之亦然
【发布时间】:2013-01-07 23:51:23
【问题描述】:

我在 iOS6 中做我的项目。我现在只有一个 UIViewController。我有一个 UIScrollView。我有几个控件(文本字段、按钮、自定义视图..等)。所以我为纵向模式构建了该项目。现在我想移动它以适应横向和纵向。为此我有这个代码

- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskAll);
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    self.scrollView.contentMode = UIViewContentModeTopRight;
self.scrollView.scrollEnabled = YES;
NSLog(@"the end point is %f",self.currentCGRectLocation.origin.y);
CGPoint scrollPoint = CGPointMake(0.0, (self.currentCGRectLocation.origin.y/500)*400);
[self.scrollView setContentOffset:scrollPoint animated:YES];

CGFloat scrollViewHeight = 0.0f;
for (UIView *view in self.scrollView.subviews)
{
    scrollViewHeight += view.frame.size.height;
}

CGSize newSize = CGSizeMake(100,scrollViewHeight);

[self.scrollView setContentSize:newSize];

self.view.userInteractionEnabled =YES;
self.scrollView.userInteractionEnabled = YES;
}

在我看来DidLoad

  - (void)viewDidLoad
{
[self.scrollView setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];

}

在我的 AppDelegate.m 文件中

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return (UIInterfaceOrientationMaskAll);
}

我的问题是我将它从纵向移动到横向,然后所有控件都完美对齐,除了滚动视图。模拟器的宽度在纵向模式下为 900,在横向模式下为 1024。因此,即使在横向模式下,我的滚动视图的滚动宽度也为 900。即使我可以轻松上下移动,它就像卡在那里一样。 我正在附上一个样机。滚动视图位于纵向(900)的末尾,几乎是横向(900)的 2/3。样机在这里:http://dl.dropbox.com/u/40597849/mockup3.png。如果您需要更多信息,请询问。谢谢..

编辑:在 UIScrollView 的 MainStoryboard 上,我也未选中“使用自动布局”复选框。如果我检查它,当我从纵向移动到横向时,我在末尾有滚动视图(如我所愿)。但是当我再次从横向移动到纵向时,屏幕会冻结。我不能上下移动。

【问题讨论】:

    标签: ios uiscrollview landscape fixed portrait


    【解决方案1】:

    这两个方法被添加到我想从纵向移动到横向的 ViewController 中。对于滚动视图,我仍然未选中“使用自动布局”复选框。

    - (NSUInteger)supportedInterfaceOrientations
    {
      return (UIInterfaceOrientationMaskAll);
    }
    -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
        ||  toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        CGRect rect = self.view.frame;
        rect.size.width = self.view.frame.size.width+245;
        rect.size.height = self.view.frame.size.height+245;
        self.scrollView.frame = rect;
    }
    

    }

    这是在 AppDelegate.m 文件中添加的

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
    return (UIInterfaceOrientationMaskAll);
    }
    

    这对我有用。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 2013-09-25
      • 1970-01-01
      • 2021-02-17
      • 2022-07-07
      • 1970-01-01
      相关资源
      最近更新 更多