https://www.cnblogs.com/fxwl/p/5961372.html

div区域

8、盒子模型的相关属性

  • margin(外边距/边界)
  • border(边框)
  • padding(内边距/填充 )
  • space、gap间隙
  • axis

http://www.cocoachina.com/ios/20141026/10045.html

https://www.jianshu.com/p/d8d29e30d2d4

layoutMarginspreservesSuperviewLayoutMargins

https://www.jianshu.com/p/4237bd89f521

UILayoutGuide的用法

https://www.tuicool.com/articles/nQ36naR

干什么?

  • 替代之前视图之间的空视图
  • 封装多个视图到一个模块

为什么要替代空视图?

先来举一个需求,在一行上面显示两个固定宽度的按钮,然后剩下的三个空间(左边按钮左边,两个按钮中间,右边按钮的右边)要宽度一样。想要实现这个需求,一般会想到加三个空的 UIView 放到那,然后让他们宽度相等,这样就可以实现这个需求了。

但是这样做的方法很笨,因为 UIView 是可以接受事件的,而我们这里用了它却只是用来占位置。而且这样做也会有性能消耗,代码可读性不太好。

http://www.cocoachina.com/ios/20141026/10045.html

setContentHuggingPriority

preferredContentSize

class HMPopOverViewC: UIViewController

extension UIView {

/* The size fitting most closely to targetSize in which the receiver's subtree can be laid out while optimally satisfying the constraints. If you want the smallest possible size, pass UILayoutFittingCompressedSize; for the largest possible size, pass UILayoutFittingExpandedSize.

Also see the comment for UILayoutPriorityFittingSizeLevel.

*/

@available(iOS 6.0, *)

open func systemLayoutSizeFitting(_ targetSize: CGSize) -> CGSize // Equivalent to sending -systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority: with UILayoutPriorityFittingSizeLevel for both priorities.

@available(iOS 8.0, *)

open func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize

}

symbolImageView.setContentHuggingPriority(.defaultLow, for: .horizontal)

titleLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)

titleLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

rightStackView.setContentHuggingPriority(.required, for: .horizontal)

stackView.setContentHuggingPriority(.required, for: .horizontal)

http://www.cocoachina.com/ios/20160229/15455.html

Auto Layout压缩阻力及内容吸附讲解

大致的意思就是我们自定义的视图在默认情况下,它的固有尺寸是返回(UIViewNoIntrinsicMetric,UIViewNoIntrinsicMetric),也就是(-1,-1),只有我们根据自定义视图本身的Content来重写该方法,我们自定义的视图才能明确的知道他在显示系统中该展示的大小。

UILabel和UIButton等这些控件,系统默认是根据他们的内容实现了固有尺寸,所以我们在使用的时候只需要确定origin或者Center它们就能正确的显示。

由此可见,固有尺寸是为了实现视图的 大小自适应 而存在的。

extension NSString {

@available(iOS 7.0, *)

open func size(withAttributes attrs: [NSAttributedStringKey : Any]? = nil) -> CGSize

@available(iOS 7.0, *)

open func draw(at point: CGPoint, withAttributes attrs: [NSAttributedStringKey : Any]? = nil)

@available(iOS 7.0, *)

open func draw(in rect: CGRect, withAttributes attrs: [NSAttributedStringKey : Any]? = nil)

}

@interface NSString (NSExtendedStringDrawing)

- (void)drawWithRect:(CGRect)rect options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);

@end

edgesForExtendedLayout

全屏布局

https://www.jianshu.com/p/879fe48b0eb7

var layoutGuide: UILayoutGuide {

if #available(iOS 11, *) {

return safeAreaLayoutGuide

} else {

return layoutMarginsGuide

}

}

var edgesForExtendedLayout: UIRectEdge { get set }

Description

The edges that you extend for your view controller.

Instead of this property, use the safe area of your view to determine which parts of your interface are occluded by other content. For more information, see the safeAreaLayoutGuide and safeAreaInsets properties of UIView.

UIViewController

@available(iOS 7.0, *)

open var edgesForExtendedLayout: UIRectEdge // Defaults to UIRectEdgeAll

@available(iOS 7.0, *)

open var extendedLayoutIncludesOpaqueBars: Bool // Defaults to NO, but bars are translucent by default on 7_0.

@available(iOS, introduced: 7.0, deprecated: 11.0, message: "Use UIScrollView's contentInsetAdjustmentBehavior instead")

open var automaticallyAdjustsScrollViewInsets: Bool // Defaults to YES

open var layoutMargins: UIEdgeInsets

self.view.layoutGuides

if #available(iOS 11.0, *){

self.table.contentInsetAdjustmentBehavior = .never

} else {

self.automaticallyAdjustsScrollViewInsets = false

}

// Called just before the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop.

- (void)viewWillLayoutSubviews

{

[super viewWillLayoutSubviews];

_codeReader.previewLayer.frame = self.view.bounds;

}

lable.preferredMaxLayoutWidth = KScreenWidth;

[footerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

因为iPhoneX的产生,原本的导航栏再也不是44了,那么会出现什么问题呢?在隐藏导航栏的页面,原本的高度要向上调整一个导航栏+状态栏的高度。那么iPhoneX的导航栏高度不是44了,我们需要手动获取,

有导航栏的,代码如下:

//获取状态栏的rect

CGRect statusRect = [[UIApplication sharedApplication] statusBarFrame];

//获取导航栏的rect

CGRect navRect = self.navigationController.navigationBar.frame;

那么导航栏+状态栏的高度

statusRect.size.height+navRect.size.height

-(float)mTabbarHeight{

//Tabbar高度

return self.tabBarController.tabBar.bounds.size.height;

}

extension UIViewController {

open var tabBarItem: UITabBarItem! // Automatically created lazily with the view controller's title if it's not set explicitly.

open var tabBarController: UITabBarController? { get } // If the view controller has a tab bar controller as its ancestor, return it. Returns nil otherwise.

}

https://www.cnblogs.com/jx66/p/6061641.html

adjustsFontSizeToFitWidth=YES.

// these next 3 properties allow the label to be autosized to fit a certain width by scaling the font size(s) by a scaling factor >= the minimum scaling factor

// and to specify how the text baseline moves when it needs to shrink the font.

open var adjustsFontSizeToFitWidth: Bool // default is NO

open var baselineAdjustment: UIBaselineAdjustment // default is UIBaselineAdjustmentAlignBaselines

@available(iOS 6.0, *)

open var minimumScaleFactor: CGFloat // default is 0.0

let size = label.text?.size(font: UIFont.boldSystemFont(ofSize: 15), maxWidth: 100)

extension String {

func size(font: UIFont, maxWidth: CGFloat = UIScreen.width) -> CGSize {

let label = UILabel()

label.text = self

label.font = font

label.numberOfLines = 0

return label.sizeThatFits(CGSize(width: maxWidth, height: CGFloat(Int.max)))

}

}

label.translatesAutoresizingMaskIntoConstraints = false

label.adjustsFontSizeToFitWidth = true

cell.layoutMargins = UIEdgeInsetsZero;

cell.separatorInset = UIEdgeInsetsZero;

CGSize size = [singleCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

UILayoutFittingExpandedSize

/* The size fitting most closely to targetSize in which the receiver's subtree can be laid out while optimally satisfying the constraints. If you want the smallest possible size, pass UILayoutFittingCompressedSize; for the largest possible size, pass UILayoutFittingExpandedSize.

Also see the comment for UILayoutPriorityFittingSizeLevel.

*/

@available(iOS 6.0, *)

open func systemLayoutSizeFitting(_ targetSize: CGSize) -> CGSize // Equivalent to sending -systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority: with UILayoutPriorityFittingSizeLevel for both priorities.

Return Value

The optimal size for the view based on the provided constraint priorities.

Discussion

Use this method when you want to prioritize the view's constraints when determining the best possible size of the view. This method does not actually change the size of the view.

self-sizing table view cell

https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights/18746930#18746930

Conceptual Description

The first 2 steps below are applicable regardless of which iOS versions you are developing for.

1. Set Up & Add Constraints

In your UITableViewCell subclass, add constraints so that the subviews of the cell have their edges pinned to the edges of the cell's contentView (most importantly to the top AND bottom edges). NOTE: don't pin subviews to the cell itself; only to the cell's contentView! Let the intrinsic content size of these subviews drive the height of the table view cell's content view by making sure the content compression resistance and content hugging constraints in the vertical dimension for each subview are not being overridden by higher-priority constraints you have added. (Huh? Click here.)

Remember, the idea is to have the cell's subviews connected vertically to the cell's content view so that they can "exert pressure" and make the content view expand to fit them. Using an example cell with a few subviews, here is a visual illustration of what some (not all!) of your constraints would need to look like:

You can imagine that as more text is added to the multi-line body label in the example cell above, it will need to grow vertically to fit the text, which will effectively force the cell to grow in height. (Of course, you need to get the constraints right in order for this to work correctly!)

Getting your constraints right is definitely the hardest and most important part of getting dynamic cell heights working with Auto Layout. If you make a mistake here, it could prevent everything else from working -- so take your time! I recommend setting up your constraints in code because you know exactly which constraints are being added where, and it's a lot easier to debug when things go wrong. Adding constraints in code can be just as easy as and significantly more powerful than Interface Builder using layout anchors, or one of the fantastic open source APIs available on GitHub.

  • If you're adding constraints in code, you should do this once from within the updateConstraints method of your UITableViewCell subclass. Note that updateConstraints may be called more than once, so to avoid adding the same constraints more than once, make sure to wrap your constraint-adding code within updateConstraints in a check for a boolean property such as didSetupConstraints (which you set to YES after you run your constraint-adding code once). On the other hand, if you have code that updates existing constraints (such as adjusting the constant property on some constraints), place this in updateConstraints but outside of the check for didSetupConstraints so it can run every time the method is called.

2. Determine Unique Table View Cell Reuse Identifiers

For every unique set of constraints in the cell, use a unique cell reuse identifier. In other words, if your cells have more than one unique layout, each unique layout should receive its own reuse identifier. (A good hint that you need to use a new reuse identifier is when your cell variant has a different number of subviews, or the subviews are arranged in a distinct fashion.)

For example, if you were displaying an email message in each cell, you might have 4 unique layouts: messages with just a subject, messages with a subject and a body, messages with a subject and a photo attachment, and messages with a subject, body, and photo attachment. Each layout has completely different constraints required to achieve it, so once the cell is initialized and the constraints are added for one of these cell types, the cell should get a unique reuse identifier specific to that cell type. This means when you dequeue a cell for reuse, the constraints have already been added and are ready to go for that cell type.

Note that due to differences in intrinsic content size, cells with the same constraints (type) may still have varying heights! Don't confuse fundamentally different layouts (different constraints) with different calculated view frames (solved from identical constraints) due to different sizes of content.

  • Do not add cells with completely different sets of constraints to the same reuse pool (i.e. use the same reuse identifier) and then attempt to remove the old constraints and set up new constraints from scratch after each dequeue. The internal Auto Layout engine is not designed to handle large scale changes in constraints, and you will see massive performance issues.

ios 布局 素材 待整理的更多相关文章

  1. iOS 常用三方类库整理

    iOS 常用三方类库整理 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https://gi ...

  2. iOS 实用博客整理(连载版)

    iOS 实用博客整理(连载版) 本博客为本人觉得不错的博客的暂存地,并不是本人所写. 1.iOS开发 如何适配iOS10? 2.UIWebView和WKWebView的比较和选择 3. 如何快速的开发 ...

  3. iOS学习笔记-精华整理

    iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...

  4. iOS学习笔记总结整理

    来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...

  5. iOS - 布局重绘机制相关方法的研究

    iOS View布局重绘机制相关方法 布局 - (void)layoutSubviews - (void)layoutIfNeeded- (void)setNeedsLayout —————————— ...

  6. 几张图弄明白ios布局中的尺寸问题

    背景 先说说逆向那事.各种曲折..各种技术过时,老老实实在啃看雪的帖子..更新会有的. 回正题,这里讨论的是在Masnory框架下的布局问题.像我这种游击队没师傅带,什么都得自己琢磨,一直没闹明白下面 ...

  7. iOS 常用开源代码整理

    本文章不定期整理. 1.AFNetworking AFNetworking 采用 NSURLConnection + NSOperation, 主要方便与服务端 API 进行数据交换, 操作简单, 功 ...

  8. iOS之UI组件整理

    作者:神兽gcc 授权本站转载. 最近把iOS里的UI组件重新整理了一遍,简单来看一下常用的组件以及它们的实现.其实现在这些组件都可以通过Storyboard很快的生成,只是要向这些组件能够变得生动起 ...

  9. iOS 开发学习资料整理(持续更新)

      “如果说我看得比别人远些,那是因为我站在巨人们的肩膀上.” ---牛顿   iOS及Mac开源项目和学习资料[超级全面] http://www.kancloud.cn/digest/ios-mac ...

随机推荐

  1. 基于Office 365 无代码工作流分析-数据源的建立!

     标准操作步骤 下面整个步骤我们是以嘉昊信息的招聘过程的整个流程为一个场景,整个的流程场景的步骤例如以下: 整个的过程,我们通过Infopath 进行对应的表单流转,然后利用Sharepoint ...

  2. js逻辑执行判断

    两个变量或者函数,如果与的关系,a && b,如果a是真则在运行b,如果a是假则不运行b了:如果是或的关系,前者是真则不运行后边的了,否则反过来. 举个例子: <span cla ...

  3. 后台进程管理工具---supervisor

    supervisor是一个linux下的进程管理工具,有时须要开发一些后台服务类的程序.这类程序通常不能由于意外挂掉.所以最好能在出现意外挂掉的情况下可以重新启动,继续服务. 之前我一直採用创建dae ...

  4. VMWare无法共享文件夹(Win7宿主机\Ubuntu14.04客户机)

    在安装VMWare tools的时候,需要执行 vmware-install.pl.在安装过程中,需要编译vmhgfs module,如果编译失败就很可能导致共享文件夹无法正常挂载. 最近,我在虚拟机 ...

  5. POJ 1477:Box of Bricks

    Box of Bricks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19949   Accepted: 8029 De ...

  6. oracle 定时器调用存储过程

    转载请说明出处:http://t22011787.iteye.com/blog/1112745 一.查询系统中的job,可以查询视图 --相关视图 select * from dba_jobs; se ...

  7. 使用Google Closure Compiler全力压缩代码(转)

    JavaScript压缩代码的重要性不言而喻,如今的压缩工具也有不少,例如YUI Compressor,Google Closure Compiler,以及现在比较红火的UglifyJS.Uglify ...

  8. springcloud 向Eureka中注册服务异常java.net.ConnectException: Connection refused: connect

    异常如下: 通过debug发现,服务端的url地址仍然是默认的http://localhost:8761/eureka/apps/,也就是说yml文件中配置没有生效,检查后发现yml中相关配置多写了一 ...

  9. MyBatis基本应用

    框架的概念: 框架(Framework)是一个提供了可重用的公共结构的半成品. 数据持久化: 数据持久化是将内存中的数据模型转换为存储模型,以及将存储模型转换为内存中的数据模型的统称. ORM(Obj ...

  10. 洛谷 P1462 通往奥格瑞玛的道路(spfa+二分搜索)(4boy)

    原题:http://www.luogu.org/problem/show?pid=1462#sub 4boy: 大意:给出n个城市,有m条路,每经过一个城市都要交钱,每经过一条道路都要扣HP,有HP上 ...