iOS 如何使用facebook开源的YogaKit(一)

Yoga是facebook开源的一个编写视图的跨平台代码,YogaKit是用于iOS开发的。它是基于 Flexbox,它让布局变得更简单。可以用它替代 iOS 的自动布局和 web 的 CSS,也可以将它当成一种通用的布局系统使用。

Yoga 最初源自 Facebook 在 2014 年的一个开源的 css 布局开源库,在 2016 年经过修改,更名为 Yoga。Yoga 支持多个平台,包括 Java、C#、C 和 Swift。
下面就讲一下Yoga在iOS开发中的使用(oc代码):
使用CocoaPods进行安装:

#这里使用1.5的版本
platform :ios, '8.0'

use_frameworks!

target 'YogaTryout' do
  pod 'YogaKit', '~> 1.5'
end

执行命令:

pod install

安装成功提示:

Analyzing dependencies
Downloading dependencies
Installing Yoga (1.5.0)
Installing YogaKit (1.5.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `YogaTryout.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 2 total pods installed.

代码中导入#import <YogaKit/UIView+Yoga.h>
先布局一个试试手

    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    view.backgroundColor = [UIColor redColor];
    [view configureLayoutWithBlock:^(YGLayout * layout) {
        layout.isEnabled = YES;
        layout.width = YGPointValue(320);
        layout.height = YGPointValue(80);
        layout.marginTop = YGPointValue(64);
        layout.marginLeft = YGPointValue(0);
    }];
    [self.view addSubview:view];
    [view.yoga applyLayoutPreservingOrigin:NO];
看了这个代码是不是感觉很像Masonry的写法,要的就是这种结果,耐心请往下看。
image.png

我将top和left都给删掉有什么结果了

    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    view.backgroundColor = [UIColor redColor];
    [view configureLayoutWithBlock:^(YGLayout * layout) {
        layout.isEnabled = YES;
        layout.width = YGPointValue(320);
        layout.height = YGPointValue(80);
    }];
    [self.view addSubview:view];
    [view.yoga applyLayoutPreservingOrigin:NO];
image.png

正常运行只是默认x,y都在起始点。
再次将代码简化

这样写大家觉得是否可行了?
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    view.backgroundColor = [UIColor redColor];
    [view configureLayoutWithBlock:^(YGLayout * layout) {
        layout.isEnabled = YES;
        layout.padding = YGPointValue(self.view.frame.size.width/2);
    }];
    [self.view addSubview:view];
    [view.yoga applyLayoutPreservingOrigin:NO];
image.png

答案是肯定的,还是能运行。padding指的是从x,y的零点开始width和height是设置值的2倍。
在开发布局中有时导航栏偶尔会带来苦恼,那我能不能在上一个代码中就添加一个top就可以避开导航栏了?

    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    view.backgroundColor = [UIColor redColor];
    [view configureLayoutWithBlock:^(YGLayout * layout) {
        layout.isEnabled = YES;
        layout.marginTop = YGPointValue(64);
        layout.padding = YGPointValue(self.view.frame.size.width/2);
    }];
    [self.view addSubview:view];
    [view.yoga applyLayoutPreservingOrigin:NO];
image.png

这也是可以的,看到这里了是不是眼中已经出现无数个希望和诗。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 178,317评论 25 709
  • 那一年,他六岁。 父亲将所有的家当打包,一一系在自行车的后座,右手推着车,左手牵着他,晃晃悠悠的走进幽深的小弄堂...
    造梦森林阅读 241评论 0 0
  • 7月也就结束了呢。 发生了,很多预料之外的事情…… 心态崩的七七八八了。
    沃雷塔尔阅读 185评论 0 0
  • 人生茫茫几十年,就像在驾驶一辆从起点奔向终点的列车,期间途径很多站,上上下下很多人。有人始终陪在你身边,有的人中途...
    大姑娘心心阅读 10,423评论 0 6
  • 图片发自简书App 菜已冷,心已凉。 不知不觉,自己早已失去了那份从容不迫,失去了自己仅有的那一点点小傲娇和优越感...
    木南南木阅读 187评论 0 0

友情链接更多精彩内容