ナビゲーションバーのタイトルを2行で表示

このQ&Aのポイント
  • ナビゲーションバーのタイトルを2行で表示する方法を教えてください
  • ObjectiveCを使ってiPhoneアプリの開発をしています。ナビゲーションバーのタイトルを2行で表示させたいのですが、うまくできません。こちらのサイトを参考に進めましたが、エラーが発生しました。
  • コードの修正を行い、タイトルの表示はできましたが、サブタイトルの表示がうまくいきません。どのようにしたら2行でタイトルを表示できるでしょうか?
回答を見る
  • ベストアンサー

ナビゲーションバーのタイトルを2行で表示

ObjctiveCを用いてiPhoneApp開発の勉強をしています。 タイトルの通り、ナビゲーションバーのタイトルを2行で表示させたいのですがうまくできないので助言をいただきたいです。 こちらのサイトを参考に進めました。 (参考)http://www.yoheim.net/blog.php?q=20121203 こちらのコードをコピペするといろいろとエラーが出てしまうのですが、 まず一行目に表示するラベルのaddSubviewするところで、 [titleView addSubview:taskMessageLabel]; のように記述されていますが、まだtaskMessageLabelは作成されていません。 また、二行目に表示するラベルのtaskMessageLabelはUILabel *taskMessageLabelとすると思うのですが、それもされていません。 いろいろと手直しをし、以下のようにしたのですが、二行目のサブタイトルしか表示されませんでした。 どのようにすれば、きちんと二行でタイトルを表示することができるのでしょうか? 回答お待ちしてます // ナビゲーションバーのtitleに表示する独自Viewを作成します。 UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)]; titleView.backgroundColor = [UIColor clearColor]; titleView.opaque = NO; // ☆☆ポイントはここ!! // 以下のように代入して、タイトルに独自Viewを表示します。 self.navigationItem.titleView = titleView; // 1行目に表示するラベルを作成して、 // 上記で作成した独自Viewに追加します。 UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 195, 20)]; titleLabel.font = [UIFont boldSystemFontOfSize:16.0f]; titleLabel.text = @"RSS"; titleLabel.textColor = [UIColor whiteColor]; titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.backgroundColor = [UIColor clearColor]; [titleView addSubview:titleLabel]; // 2行目に表示するラベルを作成して、 // 上記で作成した独自Viewに追加します。 UILabel *taskMessageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 195, 40)]; taskMessageLabel.font = [UIFont boldSystemFontOfSize:10.0f]; titleLabel.text = @"サブタイトル"; taskMessageLabel.textColor = [UIColor colorWithRed:104.0f/255.0f green:100.0f/255.0f blue:100.0f/255.0f alpha:1.0f]; taskMessageLabel.textAlignment = NSTextAlignmentCenter; taskMessageLabel.backgroundColor = [UIColor clearColor]; taskMessageLabel.adjustsFontSizeToFitWidth = YES; [titleView addSubview:taskMessageLabel];

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

titleLabel.text = @"サブタイトル"; じゃなくて taskMessageLabel.text = @"サブタイトル"; でしょう。 あと、その前の行の CGRectMake(0, 20, 195, 40) は、元のコーディング通り CGRectMake(0, 20, 195, 20) としないと表示がはみ出るでしょう。

ifonesapp
質問者

お礼

回答ありがとうございます!おひさしぶりです^^ 単純な確認不足でしたね; 元のコードによく分からない記述があり、手直しして実行はできたので何がいけないのかと不思議に思ってしまいました。 無事に二行で表示する事ができました!

関連するQ&A

  • objectiv-cで困っています。教えてください

    objectiv-cの初心者でをトライ&エラーで試しながら学習しているのですが、基本的な部分で 行き詰っており、知恵をお借りしたいと思い投稿しました。 デバイスのサイズや向きが変わってもUIViewが中央に表示させたいのですが、 UIView *uv = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; uv.contentMode = UIViewContentModeCenter; uv.backgroundColor = [UIColor redColor]; [self.view addSubview:uv]; としてみました。initWithFrame:CGRectMakeの部分で0,0を指定しているので左上に表示されるのは分かるのですが、サイズだけを指定してUIViewを生成する方法が分かりません。CGSizeMakeでしていした後どうすればよいのでしょうか? 中央に表示させるのにuv.contentMode = UIViewContentModeCenter;としたのですが、使い方が間違っているのでしょうか? 十分ではないかもしれませんが、調べてはみたのですが、解決できませんでした。 宜しくお願いします。

  • Objective-C for文でのインスタンス

    既出の質問と類似してますが、解決しないので質問します。 Objective-CのNSMutableArrayを使いラベルを複数個作りならべたいのですが、 変数iを使ってラベルに番号をつける方法がわかりません。 今のコードは NSMutableArray *tiles = [NSMutableArray array]; for( int i=0; i<25; i++ ){ UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,0, tileSize,tileSize)]; label.text=[NSString stringWithFormat:@"%d",i]; [tiles addObject:label]; [self.view addSubview:label]; } です。 これを UILabel *label%d,i = [[UILabel alloc]initWithFrame:CGRectMake(0,0, tileSize,tileSize)]; のようにして、 label0、lable1、label2、label3・・・・ というように生成するにはどうしたらいいですか? Objective-C初心者です。 よろしくお願いします。

  • iPhone SDK UIViewに追加したUIButtonが反応しな

    iPhone SDK UIViewに追加したUIButtonが反応しない。。。 インターフェイスビルダーにあるUIViewにボタンを追加した場合は、 ちゃんとボタンは反応するんですが、プログラムから追加したUIViewにボタンをおくと UIControlEventTouchUpInsideのイベントに反応しません。 一応、こちらに簡単なプロジェクトもあげておきました。 http://gemic.jp/xcode/buttonOnView.zip 詳細は、下記です。 View-Based Applicationテンプレートで作成したプロジェクト(buttonOnView) buttonOnViewViewController.h #import <UIKit/UIKit.h> @interface buttonOnViewViewController : UIViewController { UIView *theView; } @end ----------------------------------------------------------------- buttonOnViewViewController.m - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; UIButton *btn1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain]; [btn1 initWithFrame:CGRectMake(60, 90, 200, 50)]; //このボタンは反応する↓ [self.view addSubview:btn1]; [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btn1 addTarget:self action:@selector(createView:) forControlEvents:UIControlEventTouchUpInside]; } - (IBAction)createView:(UIButton*)sender { theView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; [theView setBackgroundColor:[UIColor greenColor]]; [self.view addSubview:theView]; UIButton *btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain]; [btn initWithFrame:CGRectMake(60, 150, 200, 50)]; //このボタンは反応しない↓ [theView addSubview:btn]; [btn addTarget:self action:@selector(deleteView:) forControlEvents:UIControlEventTouchUpInside]; } - (IBAction)deleteView:(UIButton*)sender { NSLog(@"touch event reaction"); [theView removeFromSuperview]; } ----------------------------------------------------------------- いろいろググってみたり、DevCenterのドキュメントを探したんですが、 思ったような情報が見つからず相当はまってます。 ネットで探して見つからないってことは、凄く初歩的なことか?? と思ってるんですが、、、 どなたか、助けていただけたらありがたいです。

  • スクロールの上でラベルをドラッグする方法について

    現在、左右スクロールと上下フリックができるViewをタップすることでラベルがでてきて、そのラベルをドラッグできるようなコードを作っています。 具体的には、ViewController のViewの上に、画像を左右にScrollできるView(ScrollView)をのせ、その上に、それらの画像を上下にフリックすると別の画像がでてくるようなView(PageView、親クラスがUISrollView)を作成しています。 そこで、上下のフリック、スクロール、タップでラベルがでてくるのはできるようになったのですが、ラベルを認識してドラッグができる(UIPanGestureRecognizer)というのができません。 もし、おわかりになられる方がいらっしゃいましたら、お手数をおかけしますがご教授頂けたら幸いです。 以下、コードです。 xcodeは5.0です。 これらはViewContorllerの中に書いてあります。 //ScrollViewの上にPageViewクラスで作ったpageを乗せてスクロールしています。pageには画像をいれています。 for (int i=1;i<4;i++) { PageView *page = [[PageView alloc] initWithFrame:self.view.bounds]; page.delegate=self; [page setImage:[NSString stringWithFormat:@"%d",i]]; page.frame = CGRectMake( self.view.frame.size.width * (i-1), 0, self.view.frame.size.width, self.view.frame.size.height ); [scrollView addSubview:page]; //pageにタップしてproductLabelがでてくるようにしています。 [page addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrlTapREcotTap:)]]; NSLog(@"tap"); //ここでラベルのproductLabelだけに触れてドラッグできるようにしたいのですが、ドラッグができません。 UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)]; [productLabel addGestureRecognizer:pan]; NSLog(@"drag"); }} //ラベルをタップにより表示したり、非表示にしたりしています。 - (void)scrlTapREcotTap:(UITapGestureRecognizer *)gestureRecognizer { if(isTappFirstTime) { //put code of hide [UIView animateWithDuration:1 animations:^{ productLabel.alpha = 0; } completion: ^(BOOL finished) { productLabel.hidden = YES; }]; isTappFirstTime = NO; NSLog(@"非表示"); } else { // put code of show [UIView animateWithDuration:1 animations:^{ productLabel = [[UILabel alloc] init]; //UILabel *label = [[UILabel alloc] init]; productLabel.frame = CGRectMake(10, 10, 100, 50); productLabel.backgroundColor = [UIColor yellowColor]; productLabel.textColor = [UIColor blueColor]; productLabel.font = [UIFont fontWithName:@"AppleGothic" size:12]; productLabel.textAlignment = UITextAlignmentCenter; productLabel.text = @"hoge"; [scrollView addSubview:productLabel]; (NSLog(@"表示")); productLabel.alpha = 1; } completion: ^(BOOL finished) { productLabel.hidden = NO; }]; isTappFirstTime = YES; } } //productLabelだけがドラッグできるようにしています。 - (void) handlePanGesture:(UIPanGestureRecognizer*) sender { CGPoint location = [sender translationInView:productLabel]; CGPoint movedPoint = CGPointMake(productLabel.center.x + location.x, productLabel.center.y + location.y); productLabel.center = movedPoint; NSLog(@"dragmethod"); [sender setTranslation:CGPointZero inView:productLabel]; } お手数おかけしますが、宜しくお願いします。

  • objective-cで困っています教えてください

    objective-cをトライ&エラーで試しながら学習をしている初心者です。タッチした所を中心にまず100×100の正方形を表示し、その後スライダーで正方形の大きさを決めてから画像のトリミングをしたいのですが、正方形が表示されないのと、タッチした位置と切り取られる位置がずれています。上記の内容で行き詰っており、知恵をお借りしたいと思い投稿しました。宜しくお願いします。 ViewController.mの内容 #import "ViewController.h" @interface ViewController (){ // UILabel *label; // UIImageView *uv; //中心の位置 float v; // X座標 float x; // Y座標 float y; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //写真の表示 CGRect rect_uv = CGRectMake(0,20, 360, 360); UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect_uv]; //画像のアスペクト比を維持しUIImageViewサイズに収まるように表示 imageView.contentMode = UIViewContentModeScaleAspectFit; UIImage *image = [UIImage imageNamed:@"hoge.png"]; [imageView setImage:image]; [self.view addSubview:imageView]; // UISliderのインスタンス化 CGRect rect_sd = CGRectMake(50, 400, 200, 10); UISlider *slider = [[UISlider alloc]initWithFrame:rect_sd]; // スライダーの最小値,最大値,値を設定 slider.minimumValue = 0; slider.maximumValue = 500; slider.value = 100; // スライドしている最中に値を調べられるようにする.デフォルトでYESだがサンプルのため slider.continuous = YES; // スライダーの値が変更されたときに呼ばれるメソッドを設定 [slider addTarget:self action:@selector(slider_ValueChanged:) forControlEvents:UIControlEventValueChanged]; // スライダーをビューに追加 [self.view addSubview:slider]; // ラベルの設置 CGRect rect_lb = CGRectMake(300, 400, 100, 30); label = [[UILabel alloc]initWithFrame:rect_lb]; [self.view addSubview:label]; // 標準ボタン例文 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(50, 450, 100, 30); [btn setTitle:@"切り取り" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(trim:) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:btn]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /** * スライダーの値が変更されたとき */ - (void)slider_ValueChanged:(id)sender { UISlider *slider = sender; // スライダーの値をログに記述 NSLog(@"スライダーの値:%f", slider.value); // ラベルのテキストを設定 //float v = slider.value; v = slider.value; label.text = [NSString stringWithFormat:@"%2.f",v]; //四角形を描画 CGContextRef context = UIGraphicsGetCurrentContext(); // コンテキストを取得 CGContextStrokeRect(context, CGRectMake(x,y,v,v)); // 四角形の描画 } /** * タッチされたとき */ - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 1.anyObjectメソッドでいずれか1つのタッチを取得 // 2.locationViewメソッドで対象となるビューのタッチした座標を取得 CGPoint p = [[touches anyObject] locationInView:self.view]; x = p.x; // X座標 y = p.y; // Y座標 NSLog(@"タップ開始 %f, %f", x, y); //四角形を描画 CGContextRef context = UIGraphicsGetCurrentContext(); // コンテキストを取得 CGContextStrokeRect(context, CGRectMake(x,y,100,100)); // 四角形の描画 } /** * 切り取りボタンが押されたとき呼ばれるtrimメソッド */ -(void)trim:(UIButton*)button{ // ここに何かの処理を記述する // (引数の button には呼び出し元のUIButtonオブジェクトが引き渡されてきます) // 切り抜き元となる画像を用意する。 //UIImage *srcImage = /* UIImagePickerなどから取得したUIImage */ UIImage *srcImage = [UIImage imageNamed:@"hoge.png"]; // 切り抜く位置を指定するCGRectを作成する。 CGRect trimArea = CGRectMake(x, y, v, v); // CoreGraphicsの機能を用いて、 // 切り抜いた画像を作成する。 CGImageRef srcImageRef = [srcImage CGImage]; CGImageRef trimmedImageRef = CGImageCreateWithImageInRect(srcImageRef, trimArea); UIImage *trimmedImage = [UIImage imageWithCGImage:trimmedImageRef]; // 画像表示例文 //UIImage *img = [UIImage imageNamed:@"hoge.png"]; UIImageView *iv = [[UIImageView alloc] initWithImage:trimmedImage]; [self.view addSubview:iv]; } @end

  • Objective-c  画面遷移について

    毎回、初歩的な質問内容で申し訳ありませんが、困っているので教えて下さい。 「FirstViewController」はUIScrollViewでスクロールさせている画面で、タッチイベントを取得する為に UIScrollView のサブクラスを作っています。 そこでタッチすると、「SecondViewController」へ固定の画面で遷移するようにしたいのですが、「SecondViewController」の画面もそのままスクロール画面で表示されてしまいます。。。 「SecondViewController」をスクロールさせず、位置x=0 y=0で表示させるにはどうしたら良いのでしょうか? 詳しく教えて頂けると、大変助かります。。 ↓FirstViewController.h↓ @interface FirstViewController : UIViewController{   MyScrollview *controller; IBOutlet UIScrollView *scrollView; } ↓FirstViewController.m↓ - (void)viewDidLoad { controller = [[ MyScrollview alloc] initWithFrame:CGRectMake(0, 0, 1100, 1100)]; self.view = controller; [super viewDidLoad]; [super loadView]; scrollView.contentSize = controller.frame.size; [scrollView addSubview:controller]; } ↓MyScrollview .h↓ @interface MyScrollview : UIView<UIScrollViewDelegate>{ } @end ↓MyScrollview .m↓ -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{ SecondViewController *svc; svc = [[SecondViewController alloc] init]; [self addSubview:[svc view]]; }

  • iPhoneのUILabelでカウントダウンしたい

    Window-based Applicationで新規にプロジェクトを作成し、applicationを 以下のように書き足しました。 しかし、これではカウントダウンせずに1だけ表示されます。 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self.window makeKeyAndVisible]; UILabel *countdown = [[UILabel alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self.window addSubview:countdown]; countdown.text = @"3"; sleep(1); countdown.text = @"2"; sleep(1); countdown.text = @"1"; return YES; } 1になるまで処理が抜けないので、これではダメなのはなんとなく理解できるのですが、 どのようにコーディングすると自動的にカウントダウンできるでしょうか? もうちょっと工夫して別スレッドからcountdown.textを更新するようにもしてみましたが、 結果は変わりませんでした。

  • iPhoneアプリプログラミング

    iPhoneアプリ開発超初心者です。 「逆引きObjective-C for iPhoneアプリ」というサイトの中の http://www.objectivec-iphone.com/UIKit/UIImageView/UIImageView.html に、画像を表示するサンプルプログラムとして - (void)viewDidLoad { [super viewDidLoad]; // UIImageViewの初期化 CGRect rect = CGRectMake(10, 10, 250, 250); UIImageView *imageView = [[UIImageView alloc]initWithFrame:rect]; // 画像の読み込み imageView.image = [UIImage imageNamed:@"lena.png"]; // UIImageViewのインスタンスをビューに追加 [self.view addSubview:imageView]; } が紹介されているのですが、 メソッド - (IBAction)executeView:(id) sender {   // 画像表示 } の中で、画面上の特定位置への画像表示を指定する方法を教えてください。 上記のサンプルプログラムとメソッドを組み合わせるようなことをするのでしょうか? なにぶん超初心者で的外れなことを言っているかもしれませんが、よろしくお願いします。

  • objective-cで困っています教えてください

    下記のようにUIViewの上にUIImageViewを配置し、そこにUIImageをUIViewContentModeScaleAspectFitで表示させています。 表示している画像タップして、座標をとりたいと考えています。 UIImageView(UIImage)上だけをタップのエリアにしたいのですが、画面全体がタップのエリアになってしまいます。また、画像のアスペクト比を維持しUIImageViewサイズに収まるように表示しているので、タップでとれたUIImageView上の座標と、画像上の座標にズレができてしまいます。UIImageViewサイズに収まるように表示された画像上の位置から元の画像の座標を取得する方法を教えてください。 #import "ViewController.h" @interface ViewController (){ UIImageView *imageView; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CGRect rect = CGRectMake(0,20, 360, 480); imageView = [[UIImageView alloc] initWithFrame:rect]; imageView.backgroundColor = [UIColor redColor]; //画像のアスペクト比を維持しUIImageViewサイズに収まるように表示 imageView.contentMode = UIViewContentModeScaleAspectFit; UIImage *image = [UIImage imageNamed:@"hoge.png"]; [imageView setImage:image]; [self.view addSubview:imageView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /** * タッチされたとき */ - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint p = [[touches anyObject] locationInView:imageView]; float x = p.x; // X座標 float y = p.y; // Y座標 NSLog(@"タップ開始 %f, %f", x, y); } @end

  • iPhone IBを使用しないでUITabBarControllerを

    iPhone IBを使用しないでUITabBarControllerを実装 - (void)applicationDidFinishLaunching:(UIApplication *)application { window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // UIViewControllerを継承したクラス page1Controller = [[Page1Controller alloc] initWithNibName:nil bundle:nil]; page2Controller = [[Page2Controller alloc] initWithNibName:nil bundle:nil]; // 質問1 tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil]; [tabBarController setViewControllers:[NSArray arrayWithObjects:page1Controller, page2Controller, nil] animated:NO];  // 質問2 [window addSubview:tabBarController.view]; tabBarController.selectedIndex = 1; } 上記プログラムなのですが、 質問1.UITabBarControllerのsetViewControllersメソッドを利用して、     UIViewControllerのインスタンスをセットしているのですが、     setViewControllersメソッドは、     UITabBarControllerのプロパティviewControllersに     値をセットするためのセッターなのでしょうか? 質問2.[window addSubview:tabBarController.view]のviewは、     UITabBarControllerが継承しているUIViewControllerの     view(ゲッター)なのでしょうか? 上記の流れで、UITabBarControllerが、UIVeiwControllerを集約?しているので、 (@property(nonatomic, copy) NSArray *viewControllersとUITabBarControllerのヘルプに明記しているので) WindowのaddSubViewにセットするのは、UITabBarControllerのUIViewControllerのviewじゃないのかな? と初心者ながらの疑問です。 継承などの理解不足があると思いますが、ご教授の程お願いいたします