iOS学习--详解UIView的 contentStretch属性

26 篇文章 0 订阅

目标

通过实例和图片理解UIView的contentStretch属性

方法

  • 通过一个图片建立一个简单的UIImageView
  • 设置它的contentStretch属性
  • 修改它的frame属性
  • 观察

测试用的图片:

新建一个 UIImageView:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"grid.png"]];

保存它的一些属性值备用 

CGSize imageSize;
imageSize.width = imageView.frame.size.width;
imageSize.height = imageView.frame.size.height;

CGSize stretchSize;
stretchSize.width = 50.0;
stretchSize.height = 100.0;

水平拉伸

设置 contentStretch 属性(一般为 0.0 到 1.0之间):

imageView.contentStretch = CGRectMake(0.0, 0.0, stretchSize.width/imageSize.width, stretchSize.height/imageSize.height);

水平拉伸:

imageView.frame = CGRectMake(10.0, 10.0, imageSize.width*1.2, imageSize.height); 

这时拉伸的区域如下:

垂直拉伸

使用同样的 contentStretch, 图片被垂直拉伸:

imageView.frame = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height*1.2);

这时拉伸的区域如下:

两个方向同时拉伸:

使用下面的 contentStretch值:

imageView.contentStretch = CGRectMake(100.0/imageSize.width, 100.0/imageSize.height, stretchSize.width/imageSize.width, stretchSize.height/imageSize.height);

水平和垂直同时拉伸:

imageView.frame = CGRectMake(0.0, 0.0, 450.0, 450.0);

实际拉伸的区域是:

不要泄露内存:

[imageView release];

结论

对于给定的 contentStretch:

有一个“拉伸”区域:

和一个“固定”区域:

相关代码:

https://gist.github.com/8038667a374da0f6a24d#file_content_stretch_test.m

图片:

http://f.cl.ly/items/050w3k342y032F0E3n29/grid.png

原文地址:http://j0ris.tumblr.com/post/7345178587/uiview-contentstretch

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
为了设计一个漂亮的Android为了设计一个漂亮的Android Studio App“我的”页面,你可以考虑以下几个步骤: Studio App“我的”页面,你可以考虑以下几个方面: 1.设计页面布局:你可以使用LinearLayout、RelativeLayout或Constraint1.页面布局:你可以使用LinearLayout、RelativeLayout或者ConstraintLayout等布局来设计你的页面。你需要考虑页面中包含哪些元素,例如头像、用户名、个人信息、设置选项等,Layout等布局方式来设计你的页面。在布局中,你需要考虑页面元素的大小、位置和排列方式,以及不同元素之间的间距和对齐方式并将它们放置在合适的位置。 2.添加控件:你可以使用TextView、ImageView、Button等控件来添加页面。 2.页面元素:你可以在页面中添加各种元素,例如TextView、ImageView、Button等。元素。例如,你可以使用ImageView来添加头像,使用TextView来添加用户名和个人信息,使用Button来添加设置你需要考虑元素的大小、颜色、字体、图标等,以及它们之间的排列方式和交互方式。 3.页面样式:你可以使用不同的样式来设计选项等。 3.设置控件属性:你可以设置控件的属性来改变它们的外观和行为。例如,你可以设置TextView的字体、你的页面,例如Material Design、Flat Design等。你需要考虑页面的整体风格和色彩搭配,颜色和大小,设置ImageView的大小和形状,设置Button的背景颜色和点击事件等。 4.添加交互功能:你可以使用Java代码来添加交互功能。例如,你可以使用setOnClickListener()方法来为Button添加点击事件,以及不同元素之间的协调性和一致性。 4.页面功能:你可以在页面中添加各种功能,例如个人信息、设置、消息通知等。使用setText()方法来改变TextView的文本内容,使用setBackgroundColor()方法来改变LinearLayout你需要考虑功能的实用性和易用性,以及它们之间的逻辑关系和交互方式。 下面是一个简单的Android Studio App的背景颜色等。 下面是一个简单的示例代码,它演示了如何使用LinearLayout、TextView和Button来设计“我的”页面设计示例,你可以参考一下: ```xml一个“我的”页面,并为Button添加了点击事件: ```java //<LinearLayout 在 Activity android中:添加以下layout代码_width ="LinearLayoutmatch_parent layout =" findViewById android(R:.id.mylayout_height_layout); ="TextViewmatch name_parent" androidView = findViewById:(R.idorientation.name="vertical_view); "> Button settingButton <ImageView = findViewById android(R:.idlayout.setting_button_width); //=" 设置100dp" TextView 的文 android:本内容layout_height name="View100.setTextdp("" John Smith android:"); src="@drawable///avatar " 为Button android添加:点击事件layout_gravity setting="Buttoncenter.setOnClickListener(new View.OnClickListener_horizontal()" { android @:Overridelayout _margin Top=" public50 voiddp onClick(View"/> v ) { <TextView // android 处:理layout点击_width="事件 wrap_content Toast" .makeText(getApplicationContext android:(), "layoutSetting_height clicked="wrap",_content Toast.LENGTH_SHORT" ).show android:text(); ="用户名 " } }); android:textSize```="20sp" android:textColor="#000000" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="个人信息" android:textSize="16sp" android:textColor="#FFFFFF" android:background="#FF04EDC9" android:layout_marginTop="50dp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设置" android:textSize="16sp" android:textColor="#FFFFFF" android:background="#FF04EDC9" android:layout_marginTop="20dp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="消息通知" android:textSize="16sp" android:textColor="#FFFFFF" android:background="#FF04EDC9" android:layout_marginTop="20dp"/> </LinearLayout> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值