美文网首页
Motion-kit使用

Motion-kit使用

作者: ShinPlus | 来源:发表于2015-09-08 15:35 被阅读238次

    Strat Motion-Kit

    gem motion-kit

    创建一个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) 宽度 对应目标 view的宽度少40(-40)

    top.equals(:header, NSLayoutAttributeBottom).plus(26) 顶部对应header的底部距离 +26

    width.equals(:header).divided_by(2).minus(12) 宽度对照header分裂为2个,每个减少 -12

    相关文章

      网友评论

          本文标题:Motion-kit使用

          本文链接:https://www.haomeiwen.com/subject/isxpcttx.html