【问题标题】:Unable to unset a layout constraint in RubyMotion using Motion-Kit gem无法使用 Motion-Kit gem 在 RubyMotion 中取消设置布局约束
【发布时间】:2015-01-27 15:27:41
【问题描述】:

我遇到以下情况。 在一种情况下,我需要在视图的左侧布局一个元素,在另一种情况下,需要在视图的右侧呈现相同的视图。这两个约束显然是冲突的。

def action_style
   diameter = rmq.device.width*0.06
   constraints do
     width.is diameter
     height.is diameter
     center_y :image
   end
   reapply do
     constraints do
       offset = rmq.device.width*0.08
       if self.even
         left.is 20
       else
         right.is -offset
       end
     end
   end
end

LeftRight 有冲突,我没有得到想要的结果。有什么方法可以禁用例如 left 约束或禁用/取消设置所有约束?

【问题讨论】:

    标签: ios ruby autolayout rubymotion ios-autolayout


    【解决方案1】:

    我没有成功取消设置/删除约束属性。 目前我通过引用 left 属性并在需要时更改其值来修复它。

      def action_style
        diameter = rmq.device.width*0.06
        offset = rmq.device.width*0.08
    
        constraints do
          width.is diameter
          height.is diameter
          center_y :image
          @left = left.is offset
        end
    
        reapply do
          constraints do
            if self.even
              @left.constant = offset
            else
              @left.constant = rmq.device.width/2-offset-diameter
            end
          end
        end
      end
    

    【讨论】:

    • 嘿,是我!抱歉,我无法及时回复您的 github 问题,很高兴 Jamon 能够提供帮助……无论如何,我认为这种方法更好。我将尝试在 MK::Constraint 类中添加一个remove 方法,以便更轻松地删除它们。
    【解决方案2】:

    我建议您完全重新设置约束。

     def action_style
       diameter = rmq.device.width * 0.06
       reapply do
         target.removeConstraints(target.constraints)
         constraints do
           width.is diameter
           height.is diameter
           center_y :image
           offset = rmq.device.width * 0.08
           if self.even
             left.is 20
           else
             right.is -offset
           end
         end
       end
     end
    

    【讨论】:

    • 我现在将所有约束设置移动到 reapply 块内,并按照您提供的方式取消设置。但是当在布局上运行 reapply! 几次以不同的样式时,冲突再次出现。这怎么可能?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    相关资源
    最近更新 更多