Method: Teacup::Layout#layout

Defined in:
lib/teacup/layout.rb

#layout(view_or_class, *teacup_settings, &block) ⇒ Object

Alter the layout of a view

For example, to alter the width and height of a carousel:

Or to layout the carousel in the default style:

You can also use this method with #subview, for example to add a new image to a carousel:

Examples:

layout(carousel, width: 500, height: 100)
layout(carousel, :default_carousel)
layout(carousel) do
  subview(UIImage, backgroundColor: UIColor.colorWithImagePattern(image)
end

Parameters:

  • instance

    The first parameter is the view that you want to layout.

  • name

    The second parameter is optional, and is the stylename to apply to the element. When using stylesheets any properties defined in the current stylesheet (see #stylesheet) for this element will be immediately applied.

  • classes

    The third parameter is optional, and is a list of style classes.

  • properties

    The fourth parameter is optional, and is a Hash of properties to apply to the view directly.

  • &block

    If a block is passed, it is evaluated such that any calls to #subview that occur within that block cause created subviews to be added to this view instead of to the top-level view.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/teacup/layout.rb', line 113

def layout(view_or_class, *teacup_settings, &block)
  view = Teacup.to_instance(view_or_class)

  # prevents the calling of restyle! until we return to this method
  should_restyle = Teacup.should_restyle_and_block

  teacup_style = Style.new

  teacup_settings.each do |setting|
    case setting
    when Symbol, String
      view.stylename = setting
    when Hash
      # override settings, but apply them to teacup_style so that it remains
      # a Teacup::Style object
      Teacup::merge_defaults(setting, teacup_style, teacup_style)
    when Enumerable
      view.style_classes = setting
    when nil
      # skip. this is so that helper methods that accept arguments like
      # stylename can pass those on to this method without having to
      # introspect the values (just set the default value to `nil`)
      #
      # long story short: tests will fail `nil` is not ignore here
    else
      raise "The argument #{setting.inspect} is not supported in Teacup::Layout::layout()"
    end
  end

  if view.is_a? Teacup::View
    view.style(teacup_style.build(view))
  else
    Teacup.apply_hash view, teacup_style.build(view)
  end

  # assign the 'teacup_next_responder', which is queried for a stylesheet if
  # one is not explicitly assigned to the view. If the view already has a
  # teacup_next_responder assigned, it's because another object is already
  # responsible for managing the view's stylesheet.
  if view.teacup_next_responder.nil? && view.is_a?(Layout)
    view.teacup_next_responder = self
  end

  if block_given?
    superview_chain << view
    begin
      # yield will not work if this is defined in the context of the
      # UIViewController `layout` class method.
      yield view
    rescue NoMethodError => e
      NSLog("Exception executing layout(#{view.inspect}) in #{self.inspect} (stylesheet=#{stylesheet})")
      raise e
    end

    superview_chain.pop
  end

  if should_restyle
    Teacup.should_restyle!
    view.restyle!
  end

  view
end