LoginSignup
54
56

More than 5 years have passed since last update.

iOSメモ - Auto Layout勘所

Last updated at Posted at 2014-09-22

Auto Layout勘所

1.まず基礎を学ぶ

参考: mixi-inc/AutoLayout入門

参考:iOS 7 Xcode 5 で始める Auto Layout 入門 #1 – 基礎知識編

2.viewのcontentSizeは、viewDidLayoutSubviewsで決まる

参考:UIScrollviewをStoryboardで設定する場合

やっぱり標準Document

参考:Auto Layout ガイド

  1. 赤色が駄目制約とかを学ぶ、点線が優先度低い制約
  2. ScrollViewに気をつけろ
  3. Debugの仕方について学ぶ

Top Layout Guide,Bottom Layout Guideに注意する。

ナビゲーションバーの高さは、iOS6で44px, iOS7で64px

4inch,3.5inchの対応について - 最大値と最小値を使う

参考:iOSアプリを作ってみる (8) - Autolayoutに振り回される その1

   Autolayout に対応した画面で UIScrollView を使う時のコツ

Debug用

    -(void)viewDidLayoutSubviews{
       NSLog(@"%@",NSStringFromCGRect(self.scrollView.frame));
       NSLog(@"%@",NSStringFromCGRect(self.webView.frame));
       [super viewDidLayoutSubviews];
    }

合わして読みたい

実践 Auto Layout

Adjust Scroll View InsetsとUnder Top Bar/Under Bottom Bar

やはりお前らのiOS7対応は間違っている(解説編)

追記【高さの座標まとめ】

iphone4s iphone5/5s iphone6 iphone6 Plus
論理上のピクセル 320 x 480 320 x 568 375 x 667 414 x 736
NavigationBar(64) 416 504 603 672
Tab Bar Item(49) 431 519 618 687
NBar and TBar(113) 367 455 554 623

図を書いた方がいいな...orz

追記1

self.automaticallyAdjustsScrollViewInsets = NO;//コレ何やねん1

Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.

→NavigationVCをラップしたVCの中の、ScrollViewがちゃんと動かん時??配置されない時にこいつをNOにしよう
→Insetsとは余白のこと

self.scrollView.translatesAutoresizingMaskIntoConstraints=YES,NO//コレ何やねん2

UIViewのpropertyから抜粋

UIView
@property(nonatomic) UIViewAutoresizing autoresizingMask
Description
An integer bit mask that determines how the receiver resizes itself when its
superview’s bounds change
Maskってなんですかね

UIViewのランドスケープの為のプロパティ!autoresizingMask にだまされた!

右下に固定させるためには、
label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
これで、上と左の幅だけをリサイズ。つまり右と下の幅は固定。

→superViewの変化を吸収して、Resizeしてくれる
→例えば,xibでYESにすると、可変なViewが出来ます。

Autolayout に対応した画面で UIScrollView を使う時のコツ

  • AutoLayout だけで作るか AutoResizingMask を組み合わせるか

追記2 UIScrollViewはAutoLayoutを使用する際

  • contentSizeをコードで代入する
self.scrollView.contentSize = self.subViewIntoScrollView.bounds.size;

追記3 同じ幅のオブジェクトを等間隔に設置したい

spaceViewを設置するのはしっくり来なかった。
aspect ratioheightwidthの制約をつける

追記4-1 UILabelについて, prefferdWidthってなにそれ

numberOfLinesプロパティにて、2行以上の文字を入れたい時に好ましい幅を設定しとかないと文字数増える潰れてHelloWor...等になり、辛いよねって話

UILabel を text の長さによって複数行にする

追記4-2 UILabelについて、AutoShrinkってなにそれ

UILabelのFrameによってFontをResizeしたいではなかろうか。

スクリーンショット 2015-05-26 3.26.31.png

目一杯縮小してもはみ出てしまう場合は、「Line Breaks」の指定にしたがって表示されます。なお、この「Autoshrink」欄にある「Tighten Letter Spacing」項目はオフにしておきましょう。縮小時に文字間隔を詰める設定ですが、関連するメソッドがiOS7で非推奨になっているので、触れないほうが良さそうです。なぜXcode5のStoryboardに残っているのかは不明です。

  • 他にも細かい点

    • adjustsFontSizeToFitWidth: こいつはnumberOfLinesが1行の時しか動きません
      • コードでFontSize指定してたら出来ないので注意

追記5 AutoLayoutDebugの秘奥義 View Hierarchy Debug

HierarchyDemo.gif

54
56
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
54
56