[IOS] add uilabel and other controls to uitoolbar (XIB/storyboard GUI)

Source: Internet
Author: User

Author: zyl910

During iOS development, you sometimes need to add uilabel and other controls to the uitoolbar. I have seen a lot of materials to complete this function in code mode, which is not easy to maintain. So I made some exploration and found the solution under the XIB/storyboard GUI.

I. Code Method

The code method is mentioned in many documents. The key is to use the initwithcustomview method of uibarbuttonitem to bind the uilabel control. The Code is as follows --

Uilabel * mylabel = [[uilabel alloc] initwithframe: cgrectmake (40366f, 20366f, 45.0f, 10.0f)]; mylabel. font = [uifont systemfontofsize: 10]; mylabel. backgroundcolor = [uicolor whitecolor]; mylabel. textalignment = uitextalignmentcenter; mylabel. TEXT = @ "AA"; uibarbuttonitem * mybuttonitem = [[uibarbuttonitem alloc] initwithcustomview: mylabel]; [Buttons addobject: mybuttonitem]; // Add text

 

From --
Http://blog.csdn.net/light_jewel/article/details/7768045
Add any controls on the uitoolbar

 

Ii. graphic interface of XIB/storyboard

In the graphic interface design of the XIB/storyboard, The uilabel cannot be placed in the uitoolbar. If you forcibly drag the uilabel to the uitoolbar, The uilabel will be placed in the upper-layer container instead of uitoolbar.
Can I use initwithcustomview for code? The graphic interface can also be implemented. Why not?
I carefully checked the uitoolbar and uibarbuttonitem attribute panels. It seems that there is no way to insert controls such as uilabel.

There is no way in the mountains and waters. I tried to drag the uiview TO THE uitoolbar and found that there was an insert character. With one click, we found that a uibarbuttonitem is automatically added to the uitoolbar, which is the uiview just inserted. Haha, look at this is the graphical interface method corresponding to initwithcustomview.
Then put uilabel in that uiview, And it was successful --

After running, a problem occurs. The background color of uiview is white, which is not consistent with the blue gradient background of uitoolbar. What should I do?
You have tried to modify attributes such as alpha and opaque of uibarbuttonitem, uiview, and uilabel repeatedly, but the problem of background inconsistency cannot be solved.
Later, in an accidental attempt, I found that the magic solution was to set the background of the uiview to a certain color (such as blue), and then set it back to "default ", the background of the uiview disappears. Run test passed --

It is estimated that when the uiview control is placed, although the background surface is default, it actually uses white. At this time, the background can be changed back to the true default by resetting the attributes.

To sum up the procedure --
1. Drag the uiview to the uitoolbar (uibarbuttonitem is automatically added to the uitoolbar, where the uiview is just inserted );
2. Drag the uilabel (or other controls) to the uiview;
3. Set the background of the uiview to a certain color (such as blue );
4. Set the background of the uiview to default.

It is easy to complete in four steps.

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.