`
gypgyp
  • 浏览: 87001 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

UIView的autoresizingMask的详细研究

阅读更多
UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5

GaoYP经过详细测试,终于验证了这几个值的意义,并发现网上找到的资料居然全都是错的。汗一个,原来大家都是互相抄,也没有检查过。

因为横向和纵向的变换方式是一样的,所以就以iPhone中更常用的纵向变换为例了:

UIViewAutoresizingNone:superview变换时,自己不作变换。

UIViewAutoresizingFlexibleHeight:上边距不变,和superview在高度上变换同等高度。
比如,superview加高100,则自己也加高100。

UIViewAutoresizingFlexibleTopMargin:高度不变。上边距弹性可变,下边距保持不变。

UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight:
这个组合的变换比较绕:
首先,下边距是不变的,但高和上边距会变,变换的计算如下,
比如superview的高度,由100加高的200。自己的下边距是50,
则去掉不变的下边距后,superview的变化比例是:(100-50)/(200-50) = 50/150 = 1/3。
则自己的上边距和高都变为越来的3倍。

UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin :
这个变换的计算就比较简单了,救是自己的上边距,高,下边距都和superview同比变换。
比如superview的高由100变为200。则自己的上边距,高,下边距也都变为原来的2倍。

上面的变换只在superview的autoresizesSubviews为YES是才会发生。autoresizesSubviews默认为YES。

同时,superview的contentMode不会影响sub view的变换。

下面是苹果的官方文档:

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/autoresizingMask

autoresizesSubviews
A Boolean value that determines whether the receiver automatically resizes its subviews when its bounds change.

@property(nonatomic) BOOL autoresizesSubviews
Discussion
When set to YES, the receiver adjusts the size of its subviews when its bounds change. The default value is YES.

Availability
Available in iOS 2.0 and later.
See Also
  @property autoresizingMask
Declared In
UIView.h
autoresizingMask
An integer bit mask that determines how the receiver resizes itself when its superview’s bounds change.

@property(nonatomic) UIViewAutoresizing autoresizingMask
Discussion
When a view’s bounds change, that view automatically resizes its subviews according to each subview’s autoresizing mask. You specify the value of this mask by combining the constants described in UIViewAutoresizing using the C bitwise OR operator. Combining these constants lets you specify which dimensions of the view should grow or shrink relative to the superview. The default value of this property is UIViewAutoresizingNone, which indicates that the view should not be resized at all.

When more than one option along the same axis is set, the default behavior is to distribute the size difference proportionally among the flexible portions. The larger the flexible portion, relative to the other flexible portions, the more it is likely to grow. For example, suppose this property includes the UIViewAutoresizingFlexibleWidth and UIViewAutoresizingFlexibleRightMargin constants but does not include the UIViewAutoresizingFlexibleLeftMargin constant, thus indicating that the width of the view’s left margin is fixed but that the view’s width and right margin may change. Thus, the view appears anchored to the left side of its superview while both the view width and the gap to the right of the view increase.

If the autoresizing behaviors do not offer the precise layout that you need for your views, you can use a custom container view and override its layoutSubviews method to position your subviews more precisely.

Availability
Available in iOS 2.0 and later.
分享到:
评论

相关推荐

    iOS UIView

    - **autoresizingMask**: 属性用于控制视图如何自动调整大小。 - **addSubview(_:)**: 方法用于将一个视图添加为当前视图的子视图。 - **removeFromSuperview()**: 方法用于将视图从其父视图中移除。 - **...

    autoresizingMask - Demo

    `autoresizingMask` 是UIView类的一个属性,它是一个枚举类型,称为UIViewAutoresizing。这个枚举提供了多个常量,每个常量代表一种自动调整大小的行为。下面是一些主要的常量及其含义: 1. `...

    CALayer-AutoresizingMask:为 iOS CALayer 添加 UIViewAutoresize 支持和快速 UIView 到 CALayer 的转换方法

    CALayer-AutoresizingMask 为 iOS CALayer 添加 UIViewAutoresize 支持和快速 UIView 到 CALayer 的转换方法 安装 pod 'CALayer-AutoresizingMask' 用法 不要在这个storyboard或XIB使用自动布局,并使用您需要的...

    IOS应用源码——一个简单的层管理.zip

    通过设置`autoresizingMask`属性,视图可以根据其父视图的大小变化自动调整自身大小。 6. **触控事件处理**:`UIView`有一个`touchesBegan(_:with:)`、`touchesMoved(_:with:)`、`touchesEnded(_:with:)`和`...

    LineDrawDemo

    7. **响应式设计**:为了适应不同屏幕尺寸,可能还会涉及 `autoresizingMask` 或 Auto Layout 来确保图形在不同大小的屏幕上正确显示。 8. **代码组织**:良好的代码组织是关键,可能使用单独的方法来处理特定类型...

    iphone UITableView详解 带实例 例子

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 260)]; view.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight); view.backgroundColor = ...

    详解iOS应用开发中autoresizing尺寸自动适应属性的用法

    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(kMargin, topSpace, kTopViewWidth, kTopViewHeight)]; topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | ...

    34-绘图饼图.zip

    在这个过程中,我们需要覆盖父类的布局属性,比如设置frame、autoresizingMask等,以便饼图能适应屏幕尺寸变化。 3. **计算扇形角度**: 饼图由多个扇形区域组成,每个扇形代表数据的一个部分。首先,我们需要根据...

    【叶-孤-城】iOS进阶指南

    UIView *yellowView = [UIView new]; yellowView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; yellowView.frame = CGRectInset(redView.bounds, 20, 20); ...

    叶孤城】iOS进阶指南

    以下是对这些知识点的详细解释: ### UI设计 #### UITableView - **基础概念**:UITableView是iOS中用于展示列表数据的控件,非常适合展示分层次的数据集合。 - **主要属性**: - `dataSource`:负责提供显示在表...

    从零开始学iOS7开发系列3-我的地盘我做主-Cha191

    hudView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [view addSubview:hudView]; if (animated) { hudView.alpha = 0.0; [UIView animateWithDuration:0.3 ...

    iphone_开发_之_窗口和视图

    - **尺寸调整**: 通过设置`autoresizingMask`属性来控制视图如何随着容器尺寸的变化而调整自身。 4. **事件处理** - **触摸事件**: 触摸事件由视图接收并响应。可以通过实现`touchesBegan:withEvent:`等方法来...

    苹果ios开发一年的工作笔记.pdf

    * UIView:iOS 中的视图基类,可以使用 initWithFrame: 方法来创建视图,设置背景图片可以使用 setImage: 方法,设置背景颜色可以使用 setBackgroundColor: 方法。 * UIWindow:iOS 中的窗口类,可以使用 ...

    ios仿腾讯视频ipad版翻页效果带气泡提示

    以下将详细阐述实现这一功能所需的关键技术和步骤。 首先,我们要理解翻页效果的核心是视图控制器间的平滑过渡。在iOS中,我们可以使用`UIStoryboardSegue`或者自定义转场动画来实现。`UIStoryboardSegue`是...

    自定义AlretVIew

    下面将详细介绍如何使用XIB来创建一个自定义`AlretView`。 首先,我们需要创建一个新的XIB文件。在Xcode中,选择`File` -&gt; `New` -&gt; `File...`,然后在模板列表中选择`User Interface` -&gt; `View`,并为这个文件命名...

    (0163)-iOS/iPhone/iPAD/iPod源代码-视图布局(View Layout)-Tool Drawer

    当用户点击三角按钮时,可以通过改变`frame`或`autoresizingMask`属性来调整工具栏的位置,实现工具栏的隐藏和显示。如果使用动画效果,可以结合`UIView`的`animate(withDuration:animations:)`方法,使过渡更加平滑...

    iphone开发一年的工作笔记

    aView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); ``` ##### 8. 定义按钮 在iOS应用中,按钮是非常基础的UI元素。 **代码示例:** ```objective-c UIButton *...

    定制UITabBar

    本文将详细介绍如何定制`UITabBar`,实现个性化的设计。 首先,我们了解`UITabBar`的基本结构。`UITabBar`由多个`UITabBarItem`组成,每个`UITabBarItem`代表一个标签,可以设置文字和图标。在自定义过程中,我们...

    ios-JPBannerView无限轮播图.zip

    下面将详细解释`JPBannerView`的相关知识点。 1. **无限轮播**: 无限轮播是一种视觉效果,让用户感觉内容在不断地循环播放,而不会到达尽头。在`JPBannerView`中,这一功能通过巧妙地排列视图实现。当用户滚动到...

    iOS开发之UIScrollView详解

    介绍:UIScrollView用于在一个小范围里显示很大的内容的控件。...// autoresizingMask – 现在基本弃用,改用autoLayout typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {  UIViewAutoresizingN

Global site tag (gtag.js) - Google Analytics