LoginSignup
23
23

More than 5 years have passed since last update.

iOSで辞書をUIの一部として表示する方法

Posted at

iOSで辞書を表示するにはUIReferenceLibraryViewControllerを使います。これがUIViewControllerのサブクラスであることからモーダルでしか使用できないと思い込んでいました。つまり、以下のようにしか使えないと思っていました。

Objective-C
UIReferenceLibraryViewController *controller
    = [[UIReferenceLibraryViewController alloc] initWithTerm:@"word"];
[self presentViewController:controller animated:YES completion:nil];

しかし、アップルのマニュアルに以下の記述がありました。
You can present this view controller modally or as part of another interface.
というわけで、以下のように既存のUIの一部として使えました。

Objective-C
[self.view addSubview:controller.view];

ただし、ナビゲーションバーにあるDoneボタンはそのままでは効きません。UIReferenceLibraryViewControllerのサブクラスを作ってdismissViewControllerAnimated:completion:メソッドをオーバーライドしましょう。Doneボタンが押されるとこのメソッドが呼ばれます。

Objective-C
@interface MyReferenceLibraryViewController : UIReferenceLibraryViewController
@end

@implementation MyReferenceLibraryViewController
-(void) dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
    [super dismissViewControllerAnimated:flag completion:completion];
    [self.view removeFromSuperview];
}
@end
23
23
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
23
23