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

看板MacDev作者 (desperado)時間9年前 (2014/08/23 00:07), 9年前編輯推噓0(001)
留言1則, 1人參與, 最新討論串2/3 (看更多)
※ 引述《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 ----------------- 按鈕2 : 會設constraint讓它自己去layout, 還是直接手動找frame啊 : y設定一樣, button2的x是 button1的x + 多少 這樣? 如果在 addSubview: 的時候 button.superview.frame 不是 CGRectZero 用 sizeToFit & setFrame: 放到左右兩側的位置,加上 button1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; button2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 應該就能得到 按鈕1 ----------------- 按鈕2 的效果 或者 auto layout @"|[button1]-(>=0)-[button2]|" 我的想法是簡單的畫面用 setFrame: 需要 i18n 或 dynamic font 則用 setConstraints: : 3) autolayout在什麼情況下最好用呀...畫面需要旋轉的情況下嗎? : 如果沒有需要旋轉, 需要用到autolayout嗎? : 抱歉這問題很笨...開發那麼久了都還不知道autolayout怎麼用...真慚愧.. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.160.64.3 ※ 文章網址: http://www.ptt.cc/bbs/MacDev/M.1408723665.A.5E1.html

08/23 21:15, , 1F
08/23 21:15, 1F
※ 編輯: cendre (118.160.79.245), 08/27/2014 00:00:00
文章代碼(AID): #1JzshHNX (MacDev)
文章代碼(AID): #1JzshHNX (MacDev)