カスタムビューがスクロールビュー上で描画されない

このQ&Aのポイント
  • Xcode 5.1.1 で Cocoa アプリケーションを作っています。NSView のサブクラスを作り、ウィンドウに配置して描画することはできるようになったのですが、このカスタムビューを NSScrollView に乗せると、描画されなくなってしまいます。
  • drawRect: が呼ばれません。initWithFrame: は呼ばれています。
  • スクロールビューに乗せる方法としては、カスタムビューを選択して Editor -> Embed In -> Scroll View とやりました。ネットでいろいろと調べたのですが、原因がわかりませんでした。どうやったら描画されるようになるのか、お分かりの方がいらっしゃいましたら、教えてください。お願いします。
回答を見る
  • ベストアンサー

カスタムビューがスクロールビュー上で描画されない

Xcode 5.1.1 で Cocoa アプリケーションを作っています。 NSView のサブクラスを作り、ウィンドウに配置して描画することはできるようになったのですが、このカスタムビューを NSScrollView に乗せると、描画されなくなってしまいます。drawRect: が呼ばれません。initWithFrame: は呼ばれています。 スクロールビューに乗せる方法としては、カスタムビューを選択して Editor -> Embed In -> Scroll View とやりました。 ネットでいろいろと調べたのですが、原因がわかりませんでした。どうやったら描画されるようになるのか、お分かりの方がいらっしゃいましたら、教えてください。お願いします。 なお、同時に次のような警告がでています。黄色マークですので実行はできますが。 Misplaced View Frame for "Clip View" will be different at run time. Size will be (0, 0) at run time but is (254, 182) in the canvas. 関係あるかどうか分かりませんが、併記しておきます。

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

  • ベストアンサー
  • harawo
  • ベストアンサー率58% (3742/6450)
回答No.2

回答の続きです。画像添付が1枚しかできないので、回答を分割しました。 Auto Layoutを有効にしたうえで、スクロールビューを使いたい場合、つぎのような設定を行います。 Interface Builderのペインで、Embed inしたViewを選択します。 下部にあるツールバーの、左から2番目をクリックすると、下図のダイアログが開きます。 矢印の先の4つの破線をクリックします。実線に変わります。 widthとheightにチェックを入れます。 「Add Constraints」ボタンを押します。

snowmaaaaan
質問者

お礼

丁寧に説明をしていただきまして、どうもありがとうございました! Auto Layout については、まったく知りませんでした。教えていただいたとおりに Add Constraints をやったところ、見事に解決しました。 本当に助かりました。

その他の回答 (1)

  • harawo
  • ベストアンサー率58% (3742/6450)
回答No.1

XIBファイルをアクティブにし、File Inspectorで、「Use Autolayout」にチェックが入っていると、おっしゃっているような警告が出ます。 Auto Layoutは、Xcode 5からCocoaプロジェクトでも有効になった機能で、デフォルトがオンです。 もともとはiOSに組みこまれた機能で、iPhoneの画面サイズが3.5インチ、4インチと複数パターンになって、これをプログラムで自動補正しようというのが目的でした。 「Auto~」と付くところを見ると、プログラマがちょっと楽できるのかなと思いがちですが、じっさいはその逆、プログラマの仕事が増える結果になります。 Auto Layoutが有効な環境では、スクロールビューの中のレイアウトにも自動補正が働きます。すなわちContentViewのサイズがフレキシブルになり、ContentView上のコンテンツとなるViewの配置から、ContentViewのサイズが求められることになります。コンテンツとなるViewひとつひとつに対して、そのサイズと、ほかのViewの間隔を設定しないと、ContentViewのサイズが{0.0, 0.0}になってしまいます。

snowmaaaaan
質問者

お礼

ありがとうございました。

関連するQ&A

  • by tomorrow morning とby this time tomorrowの違い

    1.She will have finished the book by tomorrow morning. 2.By this time next month, she will be travering in Japan. 1の訳は「彼女は明日の朝までには本を読み終わっているだろう。」ですね。では2は「来月の今頃までには」にするとおかしな訳になるので、「彼女は来月の今頃、日本を旅行しているだろう。」でいいのですか。もしこれでよいなら、by ではくatでもいいような気がするのですが、なぜbyが使われているのですか。 またby the time ~は未来完了形、by this time~ は未来進行形で使うのですか。それなら They will (arrive)at Osaka by this time tomorrow. の()内はbe arrivingになるのですか。でもそれなら意味が「彼らは明日の今頃は大阪についている(最中?)でしょう」とおかしくなるんですが・・・。 質問が多くてすみませんがよろしくお願いします。

  • 翻訳お願い致します。

    All orders are to be paid in advance as per sent pro forma invoice. We will make sure only to add items to the pro forma invoice which will be in stock at the time of picking your order. Pls. note that small variations may occur due to sold out items or to more items being available than estimated at the time of making the pro forma invoice. Any balance amount will be in your account and can be balanced with your next order. Or any surplus transfer can be arranged to be returned to your account. Warehouse:

  • 時制の疑問

    I will see to it that everything is ready in time. (すべての準備が間に合うようにいたします。) 基礎的な質問かもしれませんが、どうして、 I will see to it that everything will be ready in time. とならないのでしょうか?

  • 友人へのメールです。英訳よろしくお願いします。

    アメリカ人の友人へのメールです。自分なりに英訳したのですが添削お願いします。友人なので、あまり堅苦しくなっていないといいのですが・・・ 12月から就職活動が始まります。 Job hunting will start from December. 日本では早くても4月までは就職活動をします。 In Japan, we do it from December to April at the earliest time possible. 今、日本は就職が困難だといわれていて、12月から今よりも忙しくなってしまいます。 Recentry, it is very hard to finish job hunting in Japan, and I will be busy more. だから、そんなに長い時間は会えるかわかりませんが、できるだけ時間をつくるようにはします。 So I'm not sure that we will be able to meet at a long time, but I will try to make a time for you. 添削お願いします。

  • by which timeとby the time

    文法問題で Please come at noon, by which time I will be back in my office. が正解で、by which time の箇所が空欄になっていたのですが、選択肢の中にby the timeもあったのですが、たまたま関係形容詞の勉強で目にしたat which pointというフレーズを覚えていたので、今回の文法問題も関係形容詞だと思い、正解には至ったのですが、by the timeではダメな理由が分かりません。どうしてby the timeだといけないのでしょうか。

  • 英語を日本語に訳してください

    Shoot at least one bullet out of ur right pistol Tap the reload button At the same time click to sprint Pull back on the anolog stick to stop yourself from running Hopefully you will be doing the glitch With this you will be able to run, shoot and stand still while holding on pistol out FPSゲームの操作方法です   お願いします。 直訳でいいです。

  • たびたびすみません。また訳していただきたく。

    たびたびすみません。また訳していただきたく。 お願いに上がりました。 よろしくお願いします。 The parcel which I will be sending will not attract any duty here in India. This parcel of 20 kgs will be sent only as trade sample. And the postal people will directly hand over the parcel to you in person. And so there is no question of any customs formalities or duties involved in this particular deal. You will face all these problems only when you import large quantities. I am not at all sending this parcel as regular export but only as a sample. This time I will be sending the product only as a sample to make a trial run. I will give a letter to the postal customs department it is only a trade sample to get future bulk order. So there won't be any problem in India as well as in China. If you have any doubt and you feel that there would some problem in sending the parcel to China, I can send the parcel to your Japan address. Please inform me at the earliest.

  • 和訳をお願いいたします

    英文を和訳していただけるかたのみで お願いできますでしょうか? 抽象的な部分は意訳していただけると ありがたいです。 どうぞよろしくお願いいたします。 This relationship will eventually make all of your dreams come true, and any pitfalls will be overcome. For your love will be strong enough to overcome all things together. all trials and tests within the relationship will be overcome and in the end nothing can break you up. There will always be a bond between you that is never broken. At times will seem sometimes to exist only tenuously encroaching on the inner world of dreams and longings, intense and yet insubstantial, at other time it will be solid passionate and worldly.

  • 訳はこれでいいですか??

    Tokorode, 4 months in Japan in one year is a long time not a short time I think.??? ところで、一年に、日本にいるのが、四か月っていうのは、ながいか、短いか、どっち。 At the start of June hopefully the film will still be on. そのフィルムが7月の初旬に、まだやってたらいいね I think??はどう訳しますか? おねがいします

  • 和訳を教えて下さい。

    ある会社の製品の特許についての説明なのですが、何を言いたいのか良く分かりません。わかられる方がありあましたら教えて下さい。 We believe that small-time companies will continue to take shots at the technology because when your product stands out alone at the top of the heap, there will always be wishful people taking shots at you in an attempt to try and make themselves look better.