LoginSignup
1
2

More than 3 years have passed since last update.

UIPopoverPresentationControllerの実装のやり方(Objective-C)

Posted at

こういう人に向けて発信しています。

・UIPopoverPresentationController(Objective-C)の実装をしたい人
・UIPopOverCotnroolerを使いたく無い人
・Objective-C初心者

UIPopoverPresentationControllerとは何か

UIPopoverPresentationControllerはiOS8.0以降から実装されました。
しかし、ネット上にはSwiftばかりで全然Objective-Cの情報がない!

という事で今回Objective-Cで簡単な実装を手順まとめました。

クラス構成について

・ViewController.h
・ViewController.m
・popOverViewController
・popOverViewController.m
上記クラスのみでxibなど使わず、コードでボタンなど初期化しております。

下記に各クラスのコードを貼っておくので
書き換えて再現してみてください。

ViewController.h


#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end

ViewController.m


#import "ViewController.h"
#import "popOverViewController.h"


//デリゲートの採用はしておく。
@interface ViewController ()<UIPopoverPresentationControllerDelegate>

@end

@implementation ViewController{
    UIButton *nextVCBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initButton];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)initButton{
    nextVCBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height/2 + 50.0f, self.view.bounds.size.width, 10.0f)];
    [nextVCBtn setTitle:@"次のVCに遷移。(UIButton)" forState:UIControlStateNormal];
    [nextVCBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    // ボタンがタッチダウンされた時にnextViewメソッドを呼び出す
    [nextVCBtn addTarget:self action:@selector(nextView:)
        forControlEvents:UIControlEventTouchDown];

    [self.view addSubview:nextVCBtn];
}

- (IBAction)nextView:(id)sender {
    //表示したいViewControllerを初期化する。
    popOverViewController *popOverVC = [[popOverViewController alloc] init];
    [self presentPopOverWithViewController:popOverVC sourceView:nextVCBtn];

}

- (void)presentPopOverWithViewController:(UIViewController *)viewController sourceView:(UIView *)sourceView
{
    viewController.modalPresentationStyle = UIModalPresentationPopover;
    viewController.preferredContentSize = CGSizeMake(100.0, 100.0);

    UIPopoverPresentationController *presentationController = viewController.popoverPresentationController;
    presentationController.delegate = self;
    presentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    presentationController.sourceView = sourceView;
    presentationController.sourceRect = sourceView.bounds;

    [self presentViewController:viewController animated:YES completion:NULL];
}

//iPhoneでの描画に大きく関係するのでしっかり追加しておく
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}

@end

popOverViewController.h


#import <UIKit/UIKit.h>
@interface popOverViewController : UIViewController

@end

popOverViewController.m


#import "popOverViewController.h"

@interface popOverViewController ()

@end

@implementation popOverViewController{
    UIView *secondView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    secondView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100.0f, 0.0f)];
    secondView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:secondView];

}


@end
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2