Strat Motion-Kit
gem motion-kit
創(chuàng)建一個(gè)Controller并加載Motion-kit
class ExampleController < UIViewController
def loadView
@layout = ExampleLayout.new
self.view = @layout.view
@layout.add_constraints
end
end
新建app/layout/example_layout.rb
class ExampleController < MotionKit::Layout
def layout
end
def add_constraints
end
end
Add Constraints
Add Layout
def layout
background_color UIColor.yellowColor
add UIView, :header do
background_color UIColor.darkGrayColor
add UILabel, :header_title do
text 'Welcome'
text_alignment UITextAlignmentCenter
text_color UIColor.whiteColor
end
end
end
Add Constraionts
def add_constraints
constraints(:header) do
top_left x: 20, y:40
width.equals(view).minus(40)
height 70
end
constraints(:header_title) do
top_left x: 10, y:10
width.equals(:header).minus(20)
height.equals(:header).minus(20)
end
end
一些Constraionts
top_left x: 20, y:40
頂部與左邊
width.equals(view).minus(40)
寬度 對(duì)應(yīng)目標(biāo) view的寬度少40(-40)
top.equals(:header, NSLayoutAttributeBottom).plus(26)
頂部對(duì)應(yīng)header的底部距離 +26
width.equals(:header).divided_by(2).minus(12)
寬度對(duì)照header分裂為2個(gè)览濒,每個(gè)減少 -12