将具有分页功能的 UIScrollView 添加到现有 UIViewController

2023-11-21

我想添加一个带有分页功能的 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

这对我来说非常有效:

为您申报财产UIScrollView,并将您的 ViewController 设置为UIScrollViewDelegate

@interface ViewController : UIViewController<UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *theScrollView;

@end

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

In viewDidLoad:你的ViewController的

    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 method:

- (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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将具有分页功能的 UIScrollView 添加到现有 UIViewController 的相关文章

  • 删除 UISearchBar 中的清除按钮

    我想从 UISearchBar 中删除清除按钮 灰色 x 我尝试按照中的描述进行操作这个答案 https stackoverflow com a 19458201 但它不起作用 我将 Objective C 代码从下面的答案和评论翻译为以下
  • 如何检查dispatch_async块是否已完成运行

    所以基本上我需要能够在块完成运行后运行 segue 我有一个块可以执行一些 JSON 操作 我需要知道它何时完成运行 我有一个队列 我称之为 json queue jsonQueue dispatch queue create com ja
  • 如何设置 NSAttributedString 的字体大小

    编辑 这已被标记为重复 但正如我在下面所述 我正在寻找一个 Swift 解决方案 我发现的所有内容都是用 Objective C 编写的 我正在尝试将 HTML 转换为 NSAttributedString 但不知道如何设置字体样式和大小
  • 如何将 CGRect 转换为 MapRect

    我创建了一个如下方法将 CGRect 转换为 MapRect 如下所示 MKMapRect mapRectForRect CGRect rect CLLocationCoordinate2D topleft mapView convertP
  • 在 iOS 上将 SwiftUI 视图转换为 PDF

    我用 SwiftUI 画了一些漂亮的图表 因为它非常简单且容易做 然后我想将整个 SwiftUI 视图导出为 PDF 以便其他人可以以良好的方式查看图表 SwiftUI 没有直接为此提供解决方案 Cheers Alex 经过一番思考 我想到
  • iOS 版 Google Analytics 中的线程崩溃

    使用适用于 iOS 版本 3 0 9 以及一般的 3 0 x 的 Google Analytics 库 我们看到很多像下面这样的崩溃 它们似乎是随机发生的 Exception Type SIGBUS Exception Codes BUS
  • 还有比这更好的方法在通知附件中使用 Assets.xcassets 中的图像吗?

    我想将 Assets xcassets 中的图像附加到通知中 我已经寻找解决方案大约一个小时了 这似乎是执行此操作的唯一方法 func createLocalUrl forImageNamed name String gt URL let
  • 在 swift 中使用协议作为数组类型和函数参数

    我想创建一个可以存储符合某种协议的对象的类 对象应该存储在类型数组中 根据 Swift 文档 协议可以用作类型 因为它是一种类型 所以您可以在许多允许其他类型的地方使用协议 包括 作为函数 方法或初始值设定项中的参数类型或返回类型 作为常量
  • UIFont Woes(一些自定义字体加载,但其他字体不加载)

    我在加载某些自定义字体时遇到问题 我遵循了这个问题的 400 多个赞同的传统答案中的建议 并且它非常适合一个项目 然而 在我正在从事的另一个项目中 我遇到了加载 UIFont 的问题 这些问题与帖子中发现的问题有些相似向 UIAppFont
  • iOS 15 中表视图标题上方的额外填充

    如何更改上面的额外填充UITableViewiOS 15 中开始出现的节标题 从 iOS 15 开始 UITableView包含一个名为的新属性sectionHeaderTopPadding https developer apple co
  • Sierra 更新后无法针对 iOS 10 进行编译

    我有一个今年八月生成的证书和配置文件 它们在 Apple 开发者门户中均有效 未过期 当我打开以 8 3 为目标的项目时 出现以下错误 No certificate matching iPhone Developer My Name ABC
  • #import "xxx" 和 #import 有什么区别? [复制]

    这个问题在这里已经有答案了 可能的重复 include 和 include 文件名 有什么区别 https stackoverflow com questions 21593 what is the difference between i
  • TabBarController:以不同方向定向视图

    我无法保持当前的观点方向 在下面的设置中 我能够将第一个视图控制器锁定为纵向 将第二个视图控制器锁定为横向或纵向 但是 当我向选项卡控制器添加第二个导航控制器 rootviewcontroller 时 整个项目中的所有视图都将变为横向和纵向
  • 在 Interface Builder 中启用/禁用 NSLayoutConstraints

    NSLayoutConstraint in iOS 8 0 has a BOOL属性称为active这使得动态禁用 启用所述布局约束变得容易 要为视图控制器创建第二个布局集 然后我可以以编程方式启用 禁用它 通过IBOutletCollec
  • UIFont fontWithName:仅限于每个系列加载 2 个变体

    这个问题与带有自定义字体的 UILabel 显示 错误 自定义字体 https stackoverflow com questions 4622956 uilabel with custom font displays wrong cust
  • 搜索结果中的 Swift 搜索结果控制器连接到另一个视图控制器

    Problem 我有一个表格视图 用户可以滚动查找某些内容或使用搜索栏 搜索栏不是使用 StoryBoard 创建的 我的观点有一个UISearchController处理搜索栏和搜索结果更新 我遇到的问题是 自从我SearchResult
  • 获取 iOS Swift 中 UIViewController 的所有列表

    有没有办法获取 iOS Swift 项目中的所有 UIViewController 我想获取所有 UIViewController 的数组并检查特定的 UIViewController 是否存在 我必须找到项目中是否存在特定的 UIView
  • 处理 iPhone X 系列上 Chrome 浏览器中的安全区域

    对于我管理的网站 我正在使用新的 iPhone X 系列屏幕安全区域safe area inset
  • 在 HStack 中以正确的方式对齐两个 SwiftUI 文本视图

    我有一个包含两行的简单列表视图 每行包含两个文本视图 查看一和查看二 我想对齐每行中的最后一个标签 查看两个 以便名称标签领先对齐并保持对齐 无论字体大小如何 第一个标签 查看一个 也需要前导对齐 我尝试在第一个标签 查看一个 上设置最小框
  • 将蒙版图像作为 PNG 文件写入磁盘

    基本上 我从网络服务器下载图像 然后将它们缓存到磁盘上 但在这样做之前 我想屏蔽它们 我正在使用每个人似乎都指出的屏蔽代码 可以在这里找到 http iosdevelopertips com cocoa how to mask an ima

随机推荐