alibaba/wax

write Masonry in lua

xxfenxx opened this issue · 14 comments

When I try to write a UIButton and layout with Masonry it does not work! Who can look up in my code, it happen to crash while running!

Here:

add a UIImageView to cell.contentView

--local weakSelf = self
button:masUNDERxLINEmakeConstraints(
toblock(

       function(make)
          make:left():masUNDERxLINEequalTo(self:contentView())
          make:top():masUNDERxLINEequalTo(self:contentView())
          make:right():masUNDERxLINEequalTo(self:contentView())
          make:height():masUNDERxLINEequalTo(50)

         end,{"void","id"}
      )
 )

+[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant. Location attributes must be specified in pairs

Hello, I got the same error, please show me the solution.

  1. mas_equalTo is a macro, Wax doesn't know what it is in runtime.
  2. Just simplify your code to make.left.top.right.equalTo(self.contentView); and try again.

@majie1993
How can I write a button frame in Masonry, can you show me a demo?

@LiangyongFShen
https://github.com/SnapKit/Masonry.
Read the README.md carefully and learn from Example Demo.
If you need some Chinese tutorial, just google Masonry 用法. There are a lot of good articles.

@majie1993 masonry我会用,但是我的写法报错,你可以帮我将下面的代码用lua写出来吗?我写的报错或是不起作用。

  • (void)setmyView
    {
    UIView *view = [UIView new];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor greenColor];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(self.view);
    make.width.offset(50);
    make.height.offset(50);
    }];
    }

equalTo, offset is a block, not a method. Calling block should use luaCallBlock. (I' am trying to make calling block like calling method)

function setmyView(self)
    local view = UIView:init()
    self:view():addSubview(view)
    view:setBackgroundColor(UIColor:greenColor())
    view:masUNDERxLINEmakeConstraints(toblock(
      function ( make )
        luaCallBlock(make:center():equalTo(), self:view())
        luaCallBlock(make:width():offset(), 50)
        luaCallBlock(make:height():offset(), 50)
      end
      ,{"void", "MASConstraintMaker *"}))
end

@intheway 怎么写一个左边约束100?

luaCallBlock(make:left():offset(), 100)

It works!

In Object-C:

  • (void)setMyView
    {

    UIView *view = [UIView new];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor greenColor];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view).offset(200);
    make.left.equalTo(self.view).offset(50);
    make.width.offset(10);
    make.height.offset(10);
    }];

}

In WAX:

function setMyView( self )

local myView = UIView:init()
myView:setBackgroundColor(UIColor:cyanColor())
self:view():addSubview(myView)
myView:masUNDERxLINEmakeConstraints(
    toblock(function ( make )
            -- OC:
            -- make.top.equalTo(self.view).offset(200);
            -- Block = make.top.equalTo(self.view)
            -- Block.offset(200);

            -- wax:
            -- luaCallBlock(make:top():equalTo(), self:view()):setOffset(200)
            -- Block = luaCallBlock(make:top():equalTo(), self:view())
            -- Block:setOffset(200)
            luaCallBlock(make:top():equalTo(), self:view()):setOffset(200)

            luaCallBlock(make:left():equalTo(), self:view()):setOffset(50)

            -- luaCallBlock(make:width():offset(), 50)
            make:width():setOffset(10)  

            make:height():setOffset(10)
            end,{"void","id"})
)

end

@LiangyongFShen 你的代码显然是有问题的,你这样写不会挂的原因是 Wax catch 了错误。主要问题:

  • 应该尽量简化 Masonry 代码,减少翻译复杂度。比如:
make.left.mas_equalTo(self.contentView);
make.right.mas_equalTo(self.contentView);
make.top.mas_equalTo(self.contentView);

简化成:

// 不要在 Wax 中用 mas_equalTo,这是一个宏,除非你之前自己包装了这个函数
make.left.right.top.equal(self.contentView); 

还有

make.top.equalTo(self.view).offset(200);

如果控件是放在 self.view上的,那么简化成:

make.top.offset(200);
  • 每一个层级的调用都应该 cmd + 点击 跳过去看实现,你的 setOffset 的翻译说明你认定 offset 是一个 property,但实际上它是一个返回 block 的函数,所以必须用 @intheway 提到的方式去调用:
luaCallBlock(make:width():offset(), 50)

@majie1993 谢谢你的提醒,之后写的代码会注意这些问题,感谢你的热心回复!希望之后的使用能交流出更多心得!!!😃

Thanks for suggesting. I have made block calling easy now. (pull the master)
block can be called like a Lua function.

    view:masUNDERxLINEmakeConstraints(toblock(
      function ( make )
        make:top():equalTo()(self:view()):offset()(200);
        make:left():equalTo()(self:view()):offset()(50);
        make:width():offset()(10);
        make:height():offset()(10);
      end
      ,{"void", "MASConstraintMaker *"}))

more demo https://github.com/alibaba/wax/wiki/Block

@majie1993 masonry是有一个 setOffset方法的!😃

@LiangyongFShen 恩,我没看仔细,抱歉
offset 的 block 内部也是 在调 setOffset 赋值,这个 block 调用可以省略了