👋 嗨,你好,我是晓峰。
我是一名 Web 前端开发,目前就职于 企业微信。
我喜欢编程,也热衷基于 Swift 的 iOS 开发,最近一直在跑步。
这是我的开放生活日志 LifeLogs。
一个轻量的 iOS 布局辅助框架。
License: MIT License
description:
I am trying to add a greenView as a subview of redView, and in viewDidLayoutSubviews:, when I set its frame after the redView already set its frame, I got nothing, to fix this, I must move the code before the redView frame code.
code I use:
[self.greenView frameLayout:^(HotpotFrameLayout *layout) {
layout.top.equalTo(self.redView.top).offset(20.f);
layout.left.equalTo(self.redView.left).offset(20.f);
layout.width.equalTo(20.f);
layout.height.equalTo(20.f);
}];
ps: greenView has already added to redView as a subview.
Hotpot 太棒了,完美解决了我不喜欢 autolayout 但又嫌弃 frame 繁琐的问题……
不过我使用 Swift 开发,在使用 Hotpot 的时候遇到一个问题:
themeButton.frameLayout { (layout) in
layout?.top.equalTo()(self.view.top)?.offset()(self.view.layoutMargins.top)
}
这样的语法太难受了,于是我写了一个扩展来避免这些问题:
import Hotpot
extension HotpotFrameLayout {
@discardableResult func equalTo(_ to:CGFloat) ->HotpotFrameLayout {
return self.equalTo()(to)!
}
@discardableResult func offset(_ n:CGFloat) ->HotpotFrameLayout {
return self.offset()(n)!
}
}
虽然本质上还是 Objective-C 实现,但在 Swift 语法中使用起来也没什么明显的问题了呢。
优化后的语法:
themeButton.frameLayout { (layout) in
layout?.top.equalTo(self.view.top).offset(self.view.layoutMargins.top)
}
top 、 bottom 等返回的是 minY、 maxY,在 subview 里,实际上应该返回 0 和 height,这个问题导致所有 subview 里的 view 无法使用类似下面的写法来和 subview 对齐:
.bottom.equalTo(self.top).offset(-40)
.centerX.equalTo(self.centerX)
因为坐标系统是偏移了的……
也就是说这个框架的相对布局仅能针对除顶层外同层 view 坐标系统,如果是在 subview 里,则需要用类似这样的写法:
.bottom.equalTo(self.height).offset(-40)
.centerX.equalTo(self.width/2)
解决办法:
在 readme 里加入相关说明…… XD
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.