将带有分页的 UIScrollView 添加到现有的 UIViewController

Posted

技术标签:

【中文标题】将带有分页的 UIScrollView 添加到现有的 UIViewController【英文标题】:Add UIScrollView with Paging to Existing UIViewController 【发布时间】:2013-08-16 12:21:58 【问题描述】:

我想添加一个带有分页功能的 UIScrollView,以从我现有的视图控制器(我的应用程序的根视图)浏览不同的视图。我也有标签栏和导航栏控制器。我可以在这个视图控制器中添加一个滚动视图来完成我想要的吗?如果是这样,有人可以为我指出正确的方向吗?

这是我的视图控制器。

#import "KFBViewController.h"
#import "ListViewController.h"
#import "ActionAlertsViewController.h"
#import "MarketUpdatesViewController.h"
#import "AgStoriesViewController.h"
#import "KFBNewsViewController.h"
#import "MemberBenefits.h"
#import "SocialNetworks.h"
#import "WebViewController.h"
#import "YouTubeView.h"
#import "KFBFlickrViewController.h"
#import "RSFM.h"
#import "UAPush.h"
#import "TUSafariActivity.h"

@interface KFBViewController ()




@end

@implementation KFBViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    
        self.title = NSLocalizedString(@"Home", @"Home");
        self.tabBarItem.image = [UIImage imageNamed:@"home"];
        self.navigationController.delegate = self;
    
    return self;


- (void) showMenu

    NSURL *urlToShare = [NSURL URLWithString:@"https://itunes.apple.com/us/app/kentucky-farm-bureau/id580530986?mt=8"];
    NSArray *activityItems = @[urlToShare];
    // TUSafariActivity *activity = [[TUSafariActivity alloc] init];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
    activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypePostToWeibo, UIActivityTypeSaveToCameraRoll];

    [self presentViewController:activityVC animated:TRUE completion:nil];


- (IBAction)gotoSecondView

    YouTubeView *youTubeView = [[YouTubeView alloc] initWithNibName:@"YouTubeView" bundle:nil];
    youTubeView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:youTubeView animated:YES completion:nil];

- (IBAction)gotoPublicAffairs

    ListViewController *publicAffairs = [[ListViewController alloc]initWithStyle:UITableViewStylePlain];
    WebViewController *wvc = [[WebViewController alloc]init];
    [publicAffairs setWebViewController:wvc];
    publicAffairs.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.navigationController pushViewController:publicAffairs animated:YES];


- (IBAction)gotoActionAlerts

    ActionAlertsViewController *actionAlerts = [[ActionAlertsViewController alloc]initWithStyle:UITableViewStylePlain];
    WebViewController *wvc = [[WebViewController alloc]init];
    [actionAlerts setWebViewController:wvc];
    actionAlerts.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.navigationController pushViewController:actionAlerts animated:YES];


- (IBAction)gotoMarketUpdates

    MarketUpdatesViewController *marketUpdates = [[MarketUpdatesViewController alloc]initWithStyle:UITableViewStylePlain];
    WebViewController *wvc = [[WebViewController alloc]init];
    [marketUpdates setWebViewController:wvc];
    marketUpdates.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.navigationController pushViewController:marketUpdates animated:YES];


- (IBAction)gotoAgStories

    AgStoriesViewController *agStories = [[AgStoriesViewController alloc]initWithStyle:UITableViewStylePlain];
    WebViewController *wvc = [[WebViewController alloc]init];
    [agStories setWebViewController:wvc];
    agStories.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.navigationController pushViewController:agStories animated:YES];


- (IBAction)gotoKFBNews

    KFBNewsViewController *kfbNews = [[KFBNewsViewController alloc]initWithStyle:UITableViewStylePlain];
    WebViewController *wvc = [[WebViewController alloc]init];
    [kfbNews setWebViewController:wvc];
    kfbNews.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.navigationController pushViewController:kfbNews animated:YES];


- (IBAction)gotoMemberBenefits

    MemberBenefits *memberBenefits = [[MemberBenefits alloc] initWithNibName:nil bundle:nil];
    memberBenefits.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.navigationController pushViewController:memberBenefits animated:YES];


-(IBAction)gotoPhotos:(id)sender

    KFBFlickrViewController *photosView = [[KFBFlickrViewController alloc] initWithNibName:@"KFBFlickrViewController" bundle:nil];
    [self.navigationController pushViewController:photosView animated:YES];


- (IBAction)gotoSocialNetworks

    SocialNetworks *socialNetworks = [[SocialNetworks alloc] initWithNibName:nil bundle:nil];
    socialNetworks.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.navigationController pushViewController:socialNetworks animated:YES];


- (IBAction)gotoFarmMarkets

    RSFM *rsfm = [[RSFM alloc] initWithNibName:nil bundle:nil];
    rsfm.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.navigationController pushViewController:rsfm animated:YES];


- (IBAction)settingsButtonPressed:(id)sender

    [UAPush openApnsSettings:self animated:YES];


- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"Home";

    UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithTitle:@"\u2699" style:UIBarButtonItemStyleBordered target:self action:@selector(settingsButtonPressed:)];
    [settingsButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:24], UITextAttributeFont,nil] forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = settingsButton;

    UIBarButtonItem *systemAction = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showMenu)];
    self.navigationItem.rightBarButtonItem = systemAction;


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


-(NSUInteger)supportedInterfaceOrientations

    return UIInterfaceOrientationMaskPortrait;


@end

【问题讨论】:

【参考方案1】:

这对我来说非常有效:

为您的UIScrollView 声明一个属性,并将您的 ViewController 设置为UIScrollViewDelegate

@interface ViewController : UIViewController<UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *theScrollView;

@end

**请注意,我正在使用代码设置我的UIScrollView,但可以使用 XIB 轻松完成。

在您的 ViewController 的 viewDidLoad:

    self.theScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 300)];
    self.theScrollView.delegate = self;
    self.theScrollView.pagingEnabled = YES;
    self.theScrollView.showsHorizontalScrollIndicator = NO;     
    self.theScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.theScrollView];

    NSArray *viewArray = [NSArray arrayWithObjects://your UIViews];

    for(int i=0; i<viewArray.count; i++)
    
        CGRect frame;
        frame.origin.x = self.theScrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.theScrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        [subview addSubview:[viewArray objectAtIndex:i]];
        [self.theScrollView addSubview:subview];
    

    self.theScrollView.contentSize = CGSizeMake(self.theScrollView.frame.size.width * viewArray.count, self.theScrollView.frame.size.height);

大多数分页滚动视图也有一个与之关联的UIPageControl。为此,请覆盖此 UIScrollViewDelegate 方法:

- (void)scrollViewDidScroll:(UIScrollView *)sender 
    CGFloat pageWidth = self.theScrollView.frame.size.width;
    int page = floor((self.theScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.thePageControl.currentPage = page;

【讨论】:

它在这一行崩溃:[subview addSubview:[viewArray objectAtIndex:i]];将无法识别的选择器发送到实例 0xc18acd0 哪一行? @user2029585 您是否使用您的UIView 对象加载了viewArray?似乎它正在崩溃,因为您的 viewArray 是空的,或者充满了 nil 对象。 KFBViewController *home = [[KFBViewController alloc]initWithNibName:@"KFBViewController" bundle:nil]; HomeTwo *homeTwo = [[HomeTwo alloc]initWithNibName:@"HomeTwo" bundle:nil]; NSArray *viewArray = [NSArray arrayWithObjects:home, homeTwo, nil]; 您是否正在使用UIView 对象加载viewArray?还是UIViewController 对象?因为 'home' 似乎是一个 viewController。【参考方案2】:

我可能会尝试使用UIPageViewController,而不是常规的分页滚动视图。

Apple 的PhotoScroller sample code 提供了一个很好的例子来说明如何使用UIPageViewController

我还有一些 code on github 修改 PhotoScroller 以在 UIViewController 子类中加载 UIPageViewController。 (在 Apple 的示例代码中,UIPageViewController 是根视图控制器。)

我从来没有在每个页面中使用UIPageViewController 和不同的UIViewController 子类(PhotoScroller 示例代码在所有页面中都使用PhotoViewControllers),但我不明白为什么它不能用对代码进行少量修改。

【讨论】:

【参考方案3】:

此链接对您有帮助。这里使用了页面控制器和滚动视图 -

PageControl Example in iPhone

【讨论】:

以上是关于将带有分页的 UIScrollView 添加到现有的 UIViewController的主要内容,如果未能解决你的问题,请参考以下文章

带有分页的 UIScrollView 的约束

带有 uiscrollview 分页的 iOS 渐变动画

如何将多个 UIImageViews 添加到分页 UIScrollView?

iOS:无法在分页的 UIScrollView 中的 UITableView 之间滚动

从 ALAssetLibrary 为启用分页的 UIScrollView 添加多个 UIImages

在启用分页的情况下使用 UIScrollView