Re: [討論] 有無推薦的Autolayout教學 (用code)

作者: cendre (desperado)   2014-08-23 00:07:43
※ 引述《SwiftLang (Swift-Lang)》之銘言:
: 話說Autolayout從iOS6出到現在了...到現在都還沒搞懂它...真是慚愧
: 應該說每次看教學後自己嘗試都失敗, 覺得太難搞就放棄它了 囧
: 最近想來搞懂它, 有沒有推薦的教學呢? 因為現在的project都是用純code寫
: 所以希望不是用storyboard來教學
推薦這份文件
https://developer.apple.com/library/ios/documentation/userexperience/
conceptual/AutolayoutPG/AutoLayoutinCode/AutoLayoutinCode.html
→ yes0324: 幫縮http://ppt.cc/uTzT
: 另外我有幾個問題想問
: 1) 例如我有一個tableView在controller的superview裏面, 畫面有navigationBar
^ 假設這是指 controller.view?
: 最底下有個320 x 50的廣告, 我可以直接[[UITableView alloc] init](不設frame)
: 設定離navigationBar 0px, 離廣告0px, 左右0x (或固定320)
: 讓autolayout自己判斷tableView該有的寬高是多少嗎? 試過好多次都不成功
當三個 subviews 都已經 alloc init
self.navigationBar
self.tableView
self.adView
其中 self.navigationBar 已經用 setFrame: 設定好 (CGRect){{0, 0}, {320, 64}}
用 auto layout 的設定方式:
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
self.adView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.navigationBar];
[self.view addSubview:self.tableView];
[self.view addSubview:self.adView];
NSDictionary *views =
NSDictionaryOfVariableBindings(_navigationBar, _tableView, _adView);
NSArray *horizontal =
[NSLayoutConstraint constraintsWithVisualFormat:@"|[_tableView]|"
options:kNilOptions
metrics:nil
views:views];
[self.view addConstraints:horizontal];
NSString *fmt = @"V:[_navigationBar][_tableView][_adView(50)]|";
NSUInteger opt = NSLayoutFormatAlignAllLeft | NSLayoutFormatAlignAllRight;
NSArray *vertical =
[NSLayoutConstraint constraintsWithVisualFormat:fmt
options:opt
metrics:nil
views:views];
[self.view addConstraints:vertical];
: 2) 如果畫面上有多個元件, 你們會用autolayout還是直接設frame呢?
: 例如兩個UIButton好了
: 按鈕1
作者: yes0324 (yes0324)   2014-08-23 21:15:00

Links booklink

Contact Us: admin [ a t ] ucptt.com