【问题标题】:ScrollView is messing up in iOS 6ScrollView 在 iOS 6 中搞砸了
【发布时间】:2012-10-07 14:15:49
【问题描述】:

当我在 Xcode 中创建和设置滚动视图时,它永远不会工作。它根本不会滚动并显示我的内容!我已经编码了它的后端,但不行!我从 iOS 4 开始就一直在使用这个教程:Devx Scrollview Tutorial

现在,我认为由于新 iPhone 上的新屏幕尺寸,它在 iOS 6 中被破坏了。这是我一直在使用的后端代码:

    scrollView.frame = CGRectMake(0, 0, 320, 520);

//---set the content size of the scroll view---
[scrollView setContentSize:CGSizeMake(320, 520)];
// Do any additional setup after loading the view.

Xcode 中的滚动视图大小为 Width: 320, Height: 520。

我做错了什么?这可能是一些愚蠢的事情。

更新:

 - (void)viewDidLoad
{
    [super viewDidLoad];
      [self.overlay removeFromSuperview];
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    [nc addObserver:self selector:@selector(keyboardWillShow:) name:
     UIKeyboardWillShowNotification object:nil];

    [nc addObserver:self selector:@selector(keyboardWillHide:) name:
     UIKeyboardWillHideNotification object:nil];

    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                            action:@selector(didTapAnywhere:)];
    [self customizeAppearance];
     self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"stone.png"]];

    [scrollView setScrollEnabled:YES];
    scrollView.frame = ([UIScreen mainScreen].bounds);

    //---set the content size of the scroll view---
    [scrollView setContentSize:CGSizeMake(320, 800)];


    // Do any additional setup after loading the view.
}

【问题讨论】:

  • 您所期望的具体没有发生什么?
  • 好的,但是滚动视图中有多少内容?如果它小于您刚刚设置的内容大小的高度,则它不会滚动。通常,您将内容大小的高度设置为最后一个对象的 y 原点加上它的高度。
  • 我该怎么做呢?我向您展示了我的代码,并且滚动视图具有完全相同的值。
  • 如果你想让 scollview 滚动,将其内容大小的高度设置为更高的值。 (假设里面的物体比框架高度占用更多的空间)
  • 这么多,在代码中,我说它更高,滚动视图的值?

标签: ios xcode ios6 scrollview


【解决方案1】:

您将框架大小和内容大小设置为相同。您希望滚动视图滚动到哪里?您应该将UIScrollViewframe 设置为您希望视口在屏幕上的位置(可能是[UIScreen mainScreen].bounds),并将contentSize 设置为您希望滚动到达的总区域,听起来就像您希望这是 320x520 一样。

我编写了一个简单的示例程序。我创建了一个新的“单一视图”项目并将其放入主 VC 的 viewDidLoad 方法中:

UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 280, 424)];
UILabel *top = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 50)];
top.text = @"TOP";
top.textColor = [UIColor greenColor];
top.backgroundColor = [UIColor blackColor];
top.textAlignment = UITextAlignmentCenter;
[sv addSubview:top];

UILabel *bottom = [[UILabel alloc] initWithFrame:CGRectMake(0, 500, 280, 50)];
bottom.text = @"BOTTOM";
bottom.textColor = [UIColor greenColor];
bottom.textAlignment = UITextAlignmentCenter;
bottom.backgroundColor = [UIColor blackColor];
[sv addSubview:bottom];

sv.backgroundColor = [UIColor orangeColor];
sv.contentSize = CGSizeMake(280, 550);

[super.view addSubview:sv];

【讨论】:

  • 如果我希望内容那么高,我是否将滚动视图的高度设置为 520?
  • 不,就像我说的那样,您将滚动视图的高度设置为您希望人们一次看到的区域,并将内容大小设置为总内容大小。
【解决方案2】:
-(void)screenWasSwiped {

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[scrollpage setScrollEnabled:YES];

[scrollpage setContentSize:CGSizeMake(320, 1300)];

NSLog(@"you swiped the screen UP");

}else {
    [scrollpage setScrollEnabled:YES];

    [scrollpage setContentSize:CGSizeMake(320, 950)];

}

}

-(void)viewDidLoad {

UISwipeGestureRecognizer *swipeUP = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(screenWasSwiped)];
swipeUP.numberOfTouchesRequired = 1;
swipeUP.direction=UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUP];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 2010-10-15
    相关资源
    最近更新 更多