(109) UIButton of the use of-imageview, Titlelabel, rounded corners, etc.

Source: Internet
Author: User

UIButton is a common control, the use of the method is very basic, but there are a lot of tricks are often not noticed, this article mainly introduces some of UIButton's more advanced techniques, for the realization of image and label display aesthetics and so on.

Development often touches the lower or right side of the button with a caption, as shown in:

On the left is the upper and lower structure, right side of the structure, to achieve the two display, there are two ways.

"Button's internal controls"

First of all, we need to understand that there are ImageView and Titlelabel two controls inside the button, the inner margin of the button is divided into content (whole), title (title), image (picture) Three, so for the horizontal arrangement, can be achieved by the padding and modify the control coordinates two ways , and the vertical arrangement can only be done by modifying the coordinates, because the UIButton default is the horizontal arrangement.

① directly modify coordinates to achieve horizontal and vertical arrangement:

Self.imageView.frame = CGRectMake (< #CGFloat X#>, < #CGFloat Y#>, < #CGFloat Width#>, < #CGFloat height#>); Self.titleLabel.frame = CGRectMake (< #CGFloat X#>, < #CGFloat Y#>, < #CGFloat Width#>, < #CGFloat height#>);
The ② is modified by the inner margin:

Self.contentedgeinsets = Uiedgeinsetsmake (< #CGFloat Top#>, < #CGFloat Left#>, < #CGFloat Bottom#>, < #CGFloat right#>); Self.titleedgeinsets = Uiedgeinsetsmake (< #CGFloat Top#>, < #CGFloat Left#>, < #CGFloat Bottom#>, <# CGFloat right#>); Self.imageedgeinsets = Uiedgeinsetsmake (< #CGFloat Top#>, < #CGFloat Left#>, < #CGFloat Bottom#>, <# CGFloat right#>);
Note: The caption of the button is displayed Titlelabel, but the caption of the modified button is to use the Settitle method to set the caption of the button's different states, do not directly use Titlelabel to modify。

"Rounded corners of the implementation"

The Cornerradius property of the layer member of the ImageView can set the size of the fillet:

Self.imageView.layer.cornerRadius = 10;

"Multiple buttons to share a highlighted picture"

Sometimes multiple buttons Although the content is different, but the highlight should be the same, this time you can modify the background to achieve the highlight effect, while the default rendering (content dimmed), so as to achieve better results:

The first is to prohibit rendering.

self.adjustsimagewhendisabled = NO; self.adjustsimagewhenhighlighted = NO;
Then set the highlight background

Can be through different logic, such as selected, enable and so on.

[Self setbackgroundimage:[uiimage imagenamed:@ "TABBAR_SEPARATE_SELECTED_BG"] forstate:uicontrolstatedisabled];

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

(109) UIButton of the use of-imageview, Titlelabel, rounded corners, etc.

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.