【问题标题】:UIScrollView won't scroll when re-entering view重新进入视图时 UIScrollView 不会滚动
【发布时间】:2012-11-30 00:13:54
【问题描述】:

我有一个UIView(当然是在视图控制器中),在这个UIView 中,我有一个UIScrollViewUIScrollView 包含一些图像和按钮。其中一个按钮使用模态 Action Segue 并打开一个UIView,里面有一个MapView,底部有一个“返回”按钮,可以让你回到之前的UIView。但是在“返回”时(从 MapViewUIView),UIScrollView 不再滚动,就像在返回 MapView 之前所做的那样。就像它被锁定在顶部位置一样。我觉得这与代表有关?我不知道,我对这些还不太清楚。无论如何,对我的滚动问题有任何见解吗?谢谢!!

这是我的 .h 文件(我的滚动视图所在的位置):

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface ThirdViewController : UIViewController <UIScrollViewDelegate, UIScrollViewAccessibilityDelegate>

@property (strong, nonatomic) IBOutlet UIView *findUsView;
@property (weak, nonatomic) IBOutlet UIScrollView *findUsScrollView;
- (IBAction)callButton:(id)sender;
- (IBAction)getDirButton:(id)sender;

@end

这是我的 .m 文件(我的滚动视图所在的位置):

#import "ThirdViewController.h"

@implementation ThirdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"appBackgroundColor.png"]];

    [self.view addSubview:self.findUsScrollView];
}

-(void)viewWillAppear:(BOOL)animated{

    //this line creates the frame of the scrollview (dimensions)
    [self.findUsScrollView setFrame:CGRectMake(0, 0, 0, 750)];
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated
{
    //dimensions of content within scrollveiw. Scrolling enabaled.
    [self.findUsScrollView setScrollEnabled:YES];
    [self.findUsScrollView setContentSize:CGSizeMake(0, 751)];
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)callButton:(id)sender {

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:0000000000"]];
}

- (IBAction)getDirButton:(id)sender {

}

@end

这是地图视图的 .h 文件:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MyMapViewController : UIViewController <MKMapViewDelegate, MKAnnotation>

@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (weak, nonatomic) IBOutlet UINavigationBar *titleBarMap;
@property (weak, nonatomic) IBOutlet UIToolbar *bottomNavBarMap;
@property (weak, nonatomic) IBOutlet UIView *topTitlesOverlay;
- (IBAction)backButtonMap:(id)sender;

@end

这是地图视图的 .m 文件:

#import "MyMapViewController.h"

@implementation BountMapViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (IBAction)backButtonMap:(id)sender {

    [self dismissViewControllerAnimated:YES completion: ^(void){
    ThirdViewController *vc = (ThirdViewController *)[self presentingViewController];
    [[vc findUsScrollView] becomeFirstResponder];

}];
}

@end

【问题讨论】:

    标签: iphone uiscrollview ios6


    【解决方案1】:

    尝试将 [self.findUsScrollView setFrame:CGRectMake(0, 172, 320, 680)];[self.findUsScrollView setContentSize:CGSizeMake(320, 680)]; 移动到 viewDidLoad 作为第一个建议。您不希望每次出现视图时都调用它们。

    其次,你的 contentSize 和 frameSize 是一样的(320、680)。这意味着您的 UIScrollView 不需要滚动。请记住:要滚动 UIScrollView,它的内容大小必须大于 大于它的框架大小。这应该就是全部了,当然:setScrollEnabled: YES;

    【讨论】:

    • 好吧,事实证明,如果我在 viewDidAppear 中没有[self.findUsScrollView setContentSize:CGSizeMake(320, 680)];,那么它根本不会滚动。我刚刚注释掉了我的整个 viewDidAppear,它并没有影响任何东西,所以显然我一开始根本不需要它。还有其他想法吗?我已经搜索和搜索。哦,为什么我不需要使用 setFrame?它现在被注释掉了,没有任何影响。
    • 如果在Interface Builder中设置滚动视图,则不需要setFrame,在调用viewWillAppear时它已经设置好了。如果您的 scrollView 帧大小是 (320, 680),那么如果您希望它垂直滚动,则该 scrollView 的 contenSize 必须更大,比如说 320, 750)。尝试在 viewWillAppear 中为 scrollView 的框架大小和内容大小做一些 NSLogs。请记住:如果您希望您的 scrollView 滚动,它的内容大小必须大于它的框架大小。
    【解决方案2】:

    您对viewDidAppear 的覆盖调用[super viewWillAppear:] 而不是[super viewDidAppear:]

    【讨论】:

    • 啊,很好,我会在周一上班时解决这个问题。但这只是您注意到的一个错误吗?还是您认为这是造成问题的原因?
    猜你喜欢
    • 2013-01-20
    • 2019-06-17
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2011-05-15
    相关资源
    最近更新 更多