Autoresize UIView to fit subviews
@interface UIView (resizeToFit) -(void)resizeToFitSubviews;
-(void)resizeHightToFitSubviews;
-(void)resizeWidthToFitSubviews;
@end @implementation UIView (resizeToFit) -(void)resizeToFitSubviews
{
float w = 0;
float h = 0; for (UIView *v in [self subviews]) {
float fw = v.frame.origin.x + v.frame.size.width;
float fh = v.frame.origin.y + v.frame.size.height;
w = MAX(fw, w);
h = MAX(fh, h);
}
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, w, h)];
} -(void)resizeHightToFitSubviews{
float h = 0;
for (UIView *v in [self subviews]) {
float fh = v.frame.origin.y + v.frame.size.height;
h = MAX(fh, h);
}
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, h)];
} -(void)resizeWidthToFitSubviews{
float w = 0; for (UIView *v in [self subviews]) {
float fw = v.frame.origin.x + v.frame.size.width;
w = MAX(fw, w);
}
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, w, self.frame.size.height)];
} @end
enter image description here
Code is worth a billion words. Here is the solution: @interface UIView (UIView_Expanded) - (void)resizeToFitSubviews; @end @implementation UIView (UIView_Expanded) - (void)resizeToFitSubviews
{
// 1 - calculate size
CGRect r = CGRectZero;
for (UIView *v in [self subviews])
{
r = CGRectUnion(r, v.frame);
} // 2 - move all subviews inside
CGPoint fix = r.origin;
for (UIView *v in [self subviews])
{
v.frame = CGRectOffset(v.frame, -fix.x, -fix.y);
} // 3 - move frame to negate the previous movement
r = CGRectOffset(r, fix.x, fix.y); [self setFrame:r];
} @end版权声明:本文为博主原创文章,未经博主允许不得转载。
Autoresize UIView to fit subviews的更多相关文章
- ios 取出subviews中指定subview
for(UIView *view in subviews){ if(view.tag == 998) { //根据tag判断 } if([view isKindOfClass:[UIImageView ...
- UIView之常用方法
UIView之常用方法 将一个视图添加为子视图,并使之在最上面显示 -(void)addSubView:(UIView *)view; 将指定子视图移动到顶部 -(void)bringSubViewT ...
- UIView 坐标转换
例子1 Controller的view中有一个tableView,tableView的cell上有一个button,现在需要将button的frame转为在Controller的view中的frame ...
- iOS - UITableViewController
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITableView : UIScrollView <NSCoding> @available(iOS ...
- 对MBProgressHUD进行二次封装并精简使用
对MBProgressHUD进行二次封装并精简使用 https://github.com/jdg/MBProgressHUD 几个效果图: 以下源码是MBProgressHUD支持最新的iOS8的版本 ...
- iOS7 push/pop转场动画
前言 iOS 7之后,苹果提供了自定义转场动画的API,我们可以自己去定义任意动画效果.本篇为笔者学习push.pop自定义转场效果的笔记,如何有任何不正确或者有指导意见的,请在评论中留下您的宝贵意见 ...
- iOS 打印出视图中全部的子视图的名称
使用递归: - (void)listSubviewsOfView:(UIView *)view { // Get the subviews of the view NSArray *subviews ...
- iOS:小技巧(不断更新)
记录下一些不常用技巧,以防忘记,复制用. 1.获取当前的View在Window的frame: UIWindow * window=[[[UIApplication sharedApplication] ...
- iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法
"UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...
随机推荐
- Altium Designer chapter5总结
PCB设计环境中需要注意的如下: (1)PCB设计步骤:绘制原理图和生成网表—规划电路板—载入网表—元件布局—制定设计规则—布线—后期处理—DRC检查—信号完整性分析—gerbera文件输出 (2)P ...
- upc组队赛17 Stone Game【极小值】
Stone Game 题目链接 题目描述 Alice and Bob are always playing game! The game today is about taking out stone ...
- Learning OSG programing---osgAnimation(2)
osg::Node* createBase(const osg::Vec3& center,float radius) { ; ; *radius; *radius; osg::Vec3 v0 ...
- Maximum Subarray(最大连续子序列和)
https://leetcode.com/problems/maximum-subarray/ 思路: 如果全为负值,那么取最大值 如果有非负值,那么我们依次计算到当前位置为止的最大值.假设有n个元素 ...
- DS静态查找- 顺序-二分-索引
静态查找 静态表是只执行查找操作,而不执行插入.删除等操作的表. 现在常说的有五大查找方法:顺序查找.分块查找.索引查找.树查找.哈希查找. 后两种之前写过了二叉查找树和哈希表,现在回顾前面三种,它们 ...
- UI自动化处理文件上传
UI自动化处理文件上传 import win32guiimport win32con def set_uploader(self, file_path): sleep(2) self.file_pat ...
- 爬取王垠的博客并生成pdf
尚未完善,有待改进 #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'jiangwenwen' import pdfkit im ...
- PHP实现上传文件到服务器
<?php /**************************** *** 功能:上传文件到服务器 ****************************/ session_start() ...
- java Activiti6.0 后台 框架 spring5 SSM 工作流引擎 审批流程
1.模型管理 :web在线流程设计器.预览流程xml.导出xml.部署流程 2.流程管理 :导入导出流程资源文件.查看流程图.根据流程实例反射出流程模型.激活挂起 3.运行中流程:查看流程信息.当前任 ...
- echarts改变图例位置
只需要legend属性中修改如下几个示数即可: legend:{ orient:"horizontal", x:'right', y:' center', width:'100', ...