Masonry 在swift下的使用
Masonry在oc下使用很方便,但是在swift下,由于oc方法和property都可以使用.fuc的语法,swift下只有属性可以使用.property的语法,方法只能写成.func().因此在swift下如果直接写就只能写成
downTriangle.mas_makeConstraints { (make: MASConstraintMaker? ) in
_ = make?.left.equalTo()(self)
_ = make?.bottom.equalTo()(self)
}
skillTypeText.mas_makeConstraints {
_ = $?.top.left.equalTo()(self.containerView)?.with().offset()()
let size = NSValue(cgSize: CGSize(width:, height:))
_ = $?.size.equalTo()(size)
}
这种写法非常麻烦,也不自然。如果直接使用snapkit又需要引入新的库,所以楼主就对masonry做了些改动。改动后的写法:
downTriangle.mas_makeConstraints {
$.left.sEqualTo(self)
$.bottom.sEqualTo(self)
}
对于mas_equalTo则可以写成:
skillTypeText.mas_makeConstraints {
$.top.left.sEqualTo(self.containerView).with().sOffset()
// let size = NSValue(cgSize: CGSize(width:30, height:15))
$.size.sEqualTo(size: CGSize(width:, height:))
}
主要改动了以下几个地方:
1.mas_makeConstraints 及类似方法添加__nonnull的声明
- (NSArray *)mas_makeConstraints:( void(^ _Nonnull )(MASConstraintMaker* __nonnull make))block;
2.Masconstraint添加属性声明,本来是想写个category添加个声明,但是失败了,好像只能写在原文件上。
@property (nonatomic, strong, readonly) MASConstraint *left;
@property (nonatomic, strong, readonly) MASConstraint *top;
@property (nonatomic, strong, readonly) MASConstraint *right;
@property (nonatomic, strong, readonly) MASConstraint *bottom;
@property (nonatomic, strong, readonly) MASConstraint *leading;
@property (nonatomic, strong, readonly) MASConstraint *trailing;
@property (nonatomic, strong, readonly) MASConstraint *width;
@property (nonatomic, strong, readonly) MASConstraint *height;
@property (nonatomic, strong, readonly) MASConstraint *centerX;
@property (nonatomic, strong, readonly) MASConstraint *centerY;
@property (nonatomic, strong, readonly) MASConstraint *baseline;
3.swift下添加sEqualTo,sInset等方法
extension MASConstraint{
@discardableResult
func sEqualTo(_ attr : Any?) -> MASConstraint!{
return self.equalTo()(attr)
}
@discardableResult
func sEqualTo( point : CGPoint) -> MASConstraint!{
let value = NSValue(cgPoint:point)
return self.equalTo()(value)
}
@discardableResult
func sEqualTo( size : CGSize) -> MASConstraint!{
let value = NSValue(cgSize:size)
return self.equalTo()(value)
}
@discardableResult
func sEqualTo( inset : UIEdgeInsets) -> MASConstraint!{
let value = NSValue(uiEdgeInsets:inset)
return self.equalTo()(value)
}
@discardableResult
func sOffset(_ offset : CGFloat) -> MASConstraint!{
return self.offset()(offset)
}
@discardableResult
func sInset(_ inset : UIEdgeInsets) -> MASConstraint!{
return self.insets()(inset)
}
}
masonry使用的各类方法有很多,楼主的项目也很赶,一般也是遇到一个写一个。大致改法都类似。像nsarray+masaddition基本没用,也没必要去添加。
Masonry 在swift下的使用的更多相关文章
- iOS 11 导航栏 item 偏移问题 和 Swift 下 UIButton 设置 title、image 显示问题
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- Swift下自定义xib添加到Storyboard
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51657154 ...
- swift -- 如何在swift下,使用类似oc的pch文件功能
以前在做oc下的项目的时候,pch文件是必创建的,因为实在是太方便了,只要在build setting里面把pch的路径换成绝对路径,那么剩下的,想干什么就在里面干什么,但是swift下,可以实现这种 ...
- Objective-C与Swift下的自定义打印函数(Debug和Release)
1.Objective-C 在使用Objective-C进行开发的过程中,为了Debug会不断的设置打印函数.如下图是我们经常用的,用来测试监听方法的实现与否: NSLog(@"%s&quo ...
- Swift下多个Storyboard的项目结构
我是个比较喜欢用storyboard和xib的人.我个人的习惯就是,能用storyboard的一定不用代码手工撸.当然自己业余个人写的项目,基本上一个storyboard就搞定了.但涉及到多人合作下时 ...
- ios开发之--swift下Alamofire的使用
1,首先使用cocoapods导入,如果有不会的同学,可以去看我写的关于cocopods使用的那篇博客 2,直接上代码: a 先看下文件结构 CommonFile.swift import UIKit ...
- Swift下表和方法
本文将同时阐述Swift的方法和下标,因为根据我的理解,从某种程度上讲,下标的本质也是方法. 方法 众所周知,在支持面向对象的语言里,方法一般分为两种:实例方法和类方法(在有的语言中,「类方法」被称为 ...
- Swift下使用Xib设计界面
虽然Swift可以纯代码设计界面,不过不利用现有的可视化工具有时候有点效率低.下面是使用xib设计方法,部分代码来自网上. (1)新建View 2.新建View class 3.DemoView.sw ...
- 第12月第26天 swift 下划线
1. The _ is used to define that the parameter is not named If you have multiple _ it states that you ...
随机推荐
- CSS之定位布局(position,定位布局技巧)
css之定位 1.什么是定位:css中的position属性,position有四个值:absolute/relative/fixed/static(绝对/相对/固定/静态(默认))通过定位属性可以设 ...
- Firefox实用插件记录
之前总结过一个软件推荐的小文,用来记录一直以来在软件开发过程中遇到的各种实用的软件.后来发现里面越来越多的记录了Firefox的插件,所以今天决定单独抽出一个页面来记录Firefox的插件.因为平时大 ...
- ConcurrentHashMap源码解析
转自:http://www.iteye.com/topic/344876 ConcurrentHashMap是Java 5中支持高并发.高吞吐量的线程安全HashMap实现. 实现原理 锁分离 (Lo ...
- Scala基础 - 下划线使用指南
下划线这个符号几乎贯穿了任何一本Scala编程书籍,并且在不同的场景下具有不同的含义,绕晕了不少初学者.正因如此,下划线这个特殊符号无形中增加Scala的入门难度.本文希望帮助初学者踏平这个小山坡. ...
- Linux Network Management
Linux网络管理 (YouTube视频教程) ISO/OSI七层模型 ISO: The International Organization for Standardization 国际标准化组织 ...
- ke
#include <stdio.h> #include <stdlib.h> // For rand() and srand() #include <time.h> ...
- An Introduction to Stock Market Data Analysis with R (Part 1)
Around September of 2016 I wrote two articles on using Python for accessing, visualizing, and evalua ...
- Some 3D Graphics (rgl) for Classification with Splines and Logistic Regression (from The Elements of Statistical Learning)(转)
This semester I'm teaching from Hastie, Tibshirani, and Friedman's book, The Elements of Statistical ...
- JS中判断数组的方法
JavaScript中检测对象的方法 1.typeof操作符 这种方法对于一些常用的类型来说那算是毫无压力,比如Function.String.Number.Undefined等,但是要是检测Arra ...
- 几个页面loading样式
随手练习了几个loading样式,以后看到有意思的loading样式也会补充上.样式的兼容性建议还是去w3c上看下属性的兼容性,至少我习惯这么多,当然,w3c中文网貌似很久很久没更新过了,可能更好的还 ...