Uibutton
2016-03-30 17:21
316 查看
UIButton control states
Apr 16, 2013Here’s another example of the (slightly) hidden flexibility of UIButton. Until recently, I was not aware that, in the various
setX:forState:methods,
you can actually use a bitmask to specify multiple control states. Hopefully I’m not the only person in the world that didn’t realise this and this article will help someone else. In the documentation for
UIControl,
control states are described thusly (emphasis mine):
The state of a control; a control can have more than one state at a time. States are recognized differently depending on the control. For example, a UIButton instance may be configured (using the setImage:forState: method) to display one image
when it is in its normal state and a different image when it is highlighted.
So why would you care about this? Say you were implementing an edit button or a selection button with text, a background image and an image. You want different text and images for the selected and unselected states (e.g.
Editand
Done),
but you also want to modify the images or background images when highlighting - if you’re creating your own theme for the app then the default darkening or dimming might not be what you want.
So you actually need four images - normal, highlighted, selected and selected + highlighted.
This is achieved like so:
[self.button setBackgroundImage:normal forState:UIControlStateNormal]; [self.button setBackgroundImage:highlighted forState:UIControlStateHighlighted]; [self.button setBackgroundImage:selected forState:UIControlStateSelected]; [self.button setBackgroundImage:selectedHighlighted forState:UIControlStateSelected | UIControlStateHighlighted];
You can do a similar thing with
setImage:forStateand
setTitle:forState:(you
need to specifically set a title for
UIControlStateSelected | UIControlStateHighlighted, because otherwise the title for
UIControlStateNormalwill
be used. If you’re using different title text attributes, you also need to specifically set them for the selected + highlighted state.
Here’s a spectacularly ugly demonstration of the four states of a button:
传送门:http://commandshift.co.uk/blog/2013/04/16/uibutton-control-states/
相关文章推荐
- 小计UIWindow
- UIAlertView的使用
- iOS中重用UITableView单元格时,千万别忘了这个
- AS import工程出现Gradle sync failed: Gradle version 2.2 is required. Current version is 2.10
- Frequentist和Bayesian之间的故事
- 获得项目的绝对地址 getRequestURI,getRequestURL的区别
- [Form builder]:about SYSTEM.MESSAGE_LEVEL
- Gradle for Android(二)全局设置、自定义BuildConfig、混淆
- Halton Sequences霍尔顿序列生成器,计算Pi
- UITableView指定刷新
- AnimMontage(中文翻译)——UE4官方文档
- urllib2特点--urllib2.Request对象,定制请求头部信息
- 使用UIcollectionView实现图片轮播
- SpannableString与SpannableStringBuilder使用【原创】
- 给UITextView 头部插入视图
- 用JS生成UUID的方法实例
- iOS程序-UITableView分组展示数据 - 纯手写代码
- QuickTest Professionar 快捷键
- ArrayBlockingQueue源码浅析
- UI设计中ID软件书籍排版时目录的制作你会吗?【太原UI设计师】分享