@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的更多相关文章

  1. ios 取出subviews中指定subview

    for(UIView *view in subviews){ if(view.tag == 998) { //根据tag判断 } if([view isKindOfClass:[UIImageView ...

  2. UIView之常用方法

    UIView之常用方法 将一个视图添加为子视图,并使之在最上面显示 -(void)addSubView:(UIView *)view; 将指定子视图移动到顶部 -(void)bringSubViewT ...

  3. UIView 坐标转换

    例子1 Controller的view中有一个tableView,tableView的cell上有一个button,现在需要将button的frame转为在Controller的view中的frame ...

  4. iOS - UITableViewController

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITableView : UIScrollView <NSCoding> @available(iOS ...

  5. 对MBProgressHUD进行二次封装并精简使用

    对MBProgressHUD进行二次封装并精简使用 https://github.com/jdg/MBProgressHUD 几个效果图: 以下源码是MBProgressHUD支持最新的iOS8的版本 ...

  6. iOS7 push/pop转场动画

    前言 iOS 7之后,苹果提供了自定义转场动画的API,我们可以自己去定义任意动画效果.本篇为笔者学习push.pop自定义转场效果的笔记,如何有任何不正确或者有指导意见的,请在评论中留下您的宝贵意见 ...

  7. iOS 打印出视图中全部的子视图的名称

    使用递归: - (void)listSubviewsOfView:(UIView *)view { // Get the subviews of the view NSArray *subviews ...

  8. iOS:小技巧(不断更新)

    记录下一些不常用技巧,以防忘记,复制用. 1.获取当前的View在Window的frame: UIWindow * window=[[[UIApplication sharedApplication] ...

  9. iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法

    "UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...

随机推荐

  1. Java 语言的类、属性、方法各有哪些修饰符?简述各修饰符的区别

    1. 类的修饰符分为:可访问控制符和非访问控制符两种. 可访问控制符是:公共类修饰符 public 非访问控制符有:抽象类修饰符 abstract :最终类修饰符 final 1.公共类修饰符 pub ...

  2. MYSQL 的七种join

    建表 在这里呢我们先来建立两张有外键关联的张表. CREATE DATABASE db0206; USE db0206; CREATE TABLE `db0206`.`tbl_dept`( `id` ...

  3. eclipse的maven配置及本地仓库配置

    一.下载maven并解压 下载地址:http://maven.apache.org/download.cgi 解压后如下: 二.配置环境变量 配置MAVEN_HOME 再path中添加 安装成功 三. ...

  4. 虚拟机VMware,安装中标麒麟系统,64位的,版本6.0,并安装qt

    为了使用qt开发,安装中标麒麟系统. 虚拟机中安装,本来安装的是32位麒麟系统,结果发现qt无法安装(官网提供的是64位的run程序). qt安装的是qt-opensource-linux-x64-5 ...

  5. Day8---Python的字典类型及操作

    字典类 1.生成方法: a.介绍: 字典是键值对的集合,键值对 : 键是数据索引的扩展 b.生成方法: 使用{}  或者  dict()  a = {'a' = 1, 'b' = 2, 'c' = 3 ...

  6. 如何在linux命令行无界面下使用selenium

    1.安装Xvfb和pyvirtualdisplay sudo apt-get install xvfbpip install pyvirtualdisplay 2.安装chrome, chromedr ...

  7. vue 当前页跳转并强制刷新

    watch: { '$route'(to, from) { this.$router.go(0); } }, this.$router.push({ path: '/dashboard/XXZX?' ...

  8. 【学习总结】Python-3-转义字符

    参考: 本教程的评论区:菜鸟教程-Python3-Python数字 转义字符: 在需要在字符中使用特殊字符时,python用反斜杠()转义字符 END

  9. valueOf()对象返回值

    valueOf()对象返回值 Array数组的元素被转换为字符串,这些字符串由逗号分隔,连接在一起.其操作与 Array.toString 和 Array.join 方法相同. Boolean为Boo ...

  10. 使用Unsafe来实现自定义锁

    1.使用Unsafe类 import sun.misc.Unsafe; class UnsafePackage { private static Unsafe unsafe; static { try ...