<tfoot id='CQDv0'></tfoot>

    <small id='CQDv0'></small><noframes id='CQDv0'>

    • <bdo id='CQDv0'></bdo><ul id='CQDv0'></ul>
    1. <i id='CQDv0'><tr id='CQDv0'><dt id='CQDv0'><q id='CQDv0'><span id='CQDv0'><b id='CQDv0'><form id='CQDv0'><ins id='CQDv0'></ins><ul id='CQDv0'></ul><sub id='CQDv0'></sub></form><legend id='CQDv0'></legend><bdo id='CQDv0'><pre id='CQDv0'><center id='CQDv0'></center></pre></bdo></b><th id='CQDv0'></th></span></q></dt></tr></i><div id='CQDv0'><tfoot id='CQDv0'></tfoot><dl id='CQDv0'><fieldset id='CQDv0'></fieldset></dl></div>

    2. <legend id='CQDv0'><style id='CQDv0'><dir id='CQDv0'><q id='CQDv0'></q></dir></style></legend>

      以编程方式在 UIButton 上设置图像和文本

      Programmatically set image and text on UIButton(以编程方式在 UIButton 上设置图像和文本)
      <tfoot id='Iavov'></tfoot>
    3. <small id='Iavov'></small><noframes id='Iavov'>

      • <legend id='Iavov'><style id='Iavov'><dir id='Iavov'><q id='Iavov'></q></dir></style></legend>
              <tbody id='Iavov'></tbody>
            <i id='Iavov'><tr id='Iavov'><dt id='Iavov'><q id='Iavov'><span id='Iavov'><b id='Iavov'><form id='Iavov'><ins id='Iavov'></ins><ul id='Iavov'></ul><sub id='Iavov'></sub></form><legend id='Iavov'></legend><bdo id='Iavov'><pre id='Iavov'><center id='Iavov'></center></pre></bdo></b><th id='Iavov'></th></span></q></dt></tr></i><div id='Iavov'><tfoot id='Iavov'></tfoot><dl id='Iavov'><fieldset id='Iavov'></fieldset></dl></div>

                <bdo id='Iavov'></bdo><ul id='Iavov'></ul>
                本文介绍了以编程方式在 UIButton 上设置图像和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我需要以编程方式为正常和突出显示状态的图像以及文本创建按钮.我无法使用 Interface Builder 构建它,因为我需要在 UIScrollView 上创建按钮.这是我到目前为止的代码:

                I need to create button programatically with an image for normal and highlighted state as well text. I cannot build it using Interface Builder, because I need to create buttons over a UIScrollView. Here is the code I have so far:

                - (void)loadView {
                    CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
                    scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
                    scrollView.contentSize=CGSizeMake(320,960);
                
                    UIImageView *tempImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpeg"]];
                
                    UIImage * buttonImage = [UIImage imageNamed:@"contentlist_active.png"];
                
                    self.view=scrollView;
                    [scrollView addSubview:tempImageView2];
                
                    btn = [UIButton buttonWithType:UIButtonTypeCustom];
                    btn.frame = CGRectMake(22, 100, 277, 32);
                
                    [btn setImage:buttonImage forState:UIControlStateNormal]; 
                
                    [btn setTitle:@"hello world" forState:UIControlStateNormal];
                    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                
                    [scrollView addSubview:btn];
                
                }
                

                但是按钮上的文字没有显示.如果我将 buttonsetImage 注释掉,那么文本会完美显示,否则不会.我可以同时拥有文字和图片吗?

                But the text on the button is not showing. If I comment out the setImage for button, then text shows perfectly, otherwise not. Can I have both text and an image at the same time?

                推荐答案

                UIButtons setImage 将图像设置在标题上方,因此您将无法看到其下方的文本.因此,请尝试使用 setBackgroundImage 将图像设置为您的按钮.

                UIButtons setImage sets the image above the title so you will not be able to see the text below it. So try to set image to your button with setBackgroundImage.

                目标-C:

                [btn setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
                

                斯威夫特:

                myButton.setBackgroundImage(buttonImage, forState: .normal)
                

                这篇关于以编程方式在 UIButton 上设置图像和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                EAAccessory/EASession teardown during background(EAAccessory/EASession 在后台拆除)
                Getting an NSArray of a single attribute from an NSArray(从 NSArray 获取单个属性的 NSArray)
                ImageIO: lt;ERRORgt; JPEG Corrupt JPEG data: premature end of data segment iphone - how to catch this?(ImageIO:lt;错误gt;JPEG 损坏的 JPEG 数据:iphone 数据段过早结束 - 如何捕捉到这个?)
                How to get indexPath.row of tableView which is currently being displayed?(如何获取当前正在显示的 tableView 的 indexPath.row?)
                Xcode iOS organizer submit to app store yields quot;The archive is invalidquot; error(Xcode iOS 管理器提交到应用商店产生“存档无效;错误)
                MFMessageComposeViewController alloc returns nil(MFMessageComposeViewController alloc 返回 nil)

                  <small id='FiUGy'></small><noframes id='FiUGy'>

                  <tfoot id='FiUGy'></tfoot>
                1. <legend id='FiUGy'><style id='FiUGy'><dir id='FiUGy'><q id='FiUGy'></q></dir></style></legend>
                  <i id='FiUGy'><tr id='FiUGy'><dt id='FiUGy'><q id='FiUGy'><span id='FiUGy'><b id='FiUGy'><form id='FiUGy'><ins id='FiUGy'></ins><ul id='FiUGy'></ul><sub id='FiUGy'></sub></form><legend id='FiUGy'></legend><bdo id='FiUGy'><pre id='FiUGy'><center id='FiUGy'></center></pre></bdo></b><th id='FiUGy'></th></span></q></dt></tr></i><div id='FiUGy'><tfoot id='FiUGy'></tfoot><dl id='FiUGy'><fieldset id='FiUGy'></fieldset></dl></div>
                    <tbody id='FiUGy'></tbody>

                        • <bdo id='FiUGy'></bdo><ul id='FiUGy'></ul>