iOS ui--button UIButton details

Source: Internet
Author: User

Button Details

Article Summary:

1、内部子控件2、按钮拉伸问题    2-1、代码拉伸    2-2、无代码拉伸
Internal child controls

If you want to change the properties of a button's inner child control, you can only customize the button

    • Custom button: Adjust the frame of the inner child control
      • Mode 1: Implement Titlerectforcontentrect: And Imagerectforcontentrect: Method, return the frame of Titlelabel and ImageView respectively
      • Method 2: Set in the Layoutsubviews method
    • Inner margin
// 设置按钮内容的内边距(影响到imageView和titleLabel)@property(nonatomic)          UIEdgeInsets contentEdgeInsets;// 设置titleLabel的内边距(影响到titleLabel)@property(nonatomic)          UIEdgeInsets titleEdgeInsets;// 设置imageView的内边距(影响到imageView)@property(nonatomic)          UIEdgeInsets imageEdgeInsets;

Method of adjusting the frame of the inner child control 1 but it's a hassle to write, and there's a problem:

If the parameters inside the two methods depend on each other, but are not clear about the order of execution of the two methods, it is easy to get an exception.

It is then recommended to use Frame Method 2, which adjusts the inner child controls, to set the child control's frame in layoutsubviews.

The button actually does not need to add child controls, because there are two child controls inside the button UIImage and Uilabel, can be used directly, However, there are some points of note for these two child controls: the Iamgeview.image and Titlelabel.text of the UIButton's child controls cannot be set directly, because these two values (and perhaps other values, can be viewed by looking at the source) are required to have a set state property value, If no state is set, direct access to its property changes is invalidated.

Therefore, after the button has built-in child control properties, you need to first confirm whether you want to display the sub-state.

If you want to change the font of the child control Titilelabel, take the font assignment directly, because the font does not divide the state

Button Stretch Problem code stretch

After the button has used the background image, especially the rounded picture, if the button is stretched very large, the corner of the jagged display is very obvious, but also difficult to see.

So we need to handle the button stretching problem:

One way to use the UIImage object resizeable ... The meaning that can be stretched

瓦片模式:拉伸后,会将可拉伸的部分拷贝铺设一块一块拉伸模式:拉伸后,会直接将可拉伸部分直接拉大

Tile mode, if the stretchable part is pure monochrome, no difference from the stretching mode

    • Before IOS5
// 只拉伸中间的1x1区域- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;

    • IOS5 start
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
最后一般项目中这个图片拉伸的复用的功能我们可以封装在内部,我们先将这个功能封装进方法,方法名是:resizableImage那么写在谁的内部比较合适,也就是谁做这个功能最合适呢?当然是UIImage,那么就有两种方式,    1、分类类别拓展UIImage的方法,2、在继承UIImage自定义类中写注意:类别拓展拓展成类方法比较合适哦,不然你还需要通过alloc-init创建UIImage对象,然后调用这个拓展的类别方法。
No code stretching

Set on Xcode, use directly, without writing code, once and for all. Even the previous import category file name is not required.

iOS ui--button UIButton details

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.