【问题标题】:Set a button background image iPhone programmatically以编程方式设置按钮背景图像 iPhone
【发布时间】:2011-04-13 00:52:26
【问题描述】:

在 Xcode 中,如何将UIButton 的背景设置为图像?或者,如何在UIButton 中设置背景渐变?

【问题讨论】:

    标签: ios iphone xcode image button


    【解决方案1】:

    tableViewCellcollectionViewCell 中设置图像时,这对我有用:

    将以下代码放入您的cellForRowAtIndexPathcellForItemAtIndexPath

    // 获取指向单元格的指针。答案假定您已经这样做了,但这里是为了完整性。

    CheeseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cheeseCell" forIndexPath:indexPath];
    

    // 从文档库中抓取图片并设置到单元格中。

    UIImage *myCheese = [UIImage imageNamed:@"swissCheese.png"];
    [cell.cheeseThumbnail setImage:myCheese forState:UIControlStateNormal];
    

    注意: xCode 似乎对我很感兴趣。我不得不重新启动 xCode 和模拟器,它工作正常。

    这假设您已将 cheeseThumbnail 设置为 IBOutlet...以及其他一些东西...希望您对表格/集合视图足够熟悉并且可以适应它。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      Swift 3.0中按钮的背景图片代码

      buttonName.setBackgroundImage(UIImage(named: "facebook.png"), for: .normal)

      希望这会对某人有所帮助。

      【讨论】:

        【解决方案3】:

        完整代码:

        + (UIButton *)buttonWithTitle:(NSString *)title
                               target:(id)target
                             selector:(SEL)selector
                                frame:(CGRect)frame
                                image:(UIImage *)image
                         imagePressed:(UIImage *)imagePressed
                        darkTextColor:(BOOL)darkTextColor
        {
            UIButton *button = [[UIButton alloc] initWithFrame:frame];
        
            button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        
            UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
            [button setBackgroundImage:newImage forState:UIControlStateNormal];
        
            UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
            [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
        
            [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
        
            // in case the parent view draws with a custom color or gradient, use a transparent color
            button.backgroundColor = [UIColor clearColor];
        
            return button;
        }
        
        UIImage *buttonBackground = UIImage imageNamed:@"whiteButton.png";
        UIImage *buttonBackgroundPressed = UIImage imageNamed:@"blueButton.png";
        
        CGRect frame = CGRectMake(0.0, 0.0, kStdButtonWidth, kStdButtonHeight);
        
        UIButton *button = [FinishedStatsView buttonWithTitle:title
                                                       target:target
                                                     selector:action
                                                        frame:frame
                                                        image:buttonBackground
                                                 imagePressed:buttonBackgroundPressed
                                                darkTextColor:YES];
        
        [self addSubview:button]; 
        

        设置图片:

        UIImage *buttonImage = [UIImage imageNamed:@"Home.png"];
        [myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
        [self.view addSubview:myButton];
        

        要删除图像:

        [button setBackgroundImage:nil forState:UIControlStateNormal];
        

        【讨论】:

        • 为什么在将按钮的背景设置为该图像之前创建图像的副本?如果我们不这样做,我们是否会冒内存泄漏或异常行为的风险,还是只是为了让图像拉伸以填充按钮?
        【解决方案4】:

        我们可以在一行中使用此代码设置图像

        [buttonName setBackgroundImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];
        

        【讨论】:

          【解决方案5】:

          斯威夫特

          像这样设置按钮图像:

          let myImage = UIImage(named: "myImageName")
          myButton.setImage(myImage , forState: UIControlState.Normal)
          

          其中myImageName 是您的资产目录中的图片名称。

          【讨论】:

            【解决方案6】:

            如果 setBackgroundImage 对我没有帮助,但 setImage 对我有帮助

            【讨论】:

            • 使用 setImage 设置前景图像...如果要设置背景图像,请使用 setBackgroundImage: forState:
            【解决方案7】:

            无需任何代码即可设置背景图片!

            只需在 Main.storyboard 中按您想要图像的按钮,然后在右侧的 实用程序 栏中,按 属性检查器 并将背景设置为您想要的图像! 确保您在左侧的支持文件中有您想要的图片。

            【讨论】:

              【解决方案8】:

              这会起作用

              UIImage *buttonImage = [UIImage imageNamed:@"imageName.png"];
              [btn setImage:buttonImage forState:UIControlStateNormal];
              [self.view addSubview:btn];
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-06-12
                • 1970-01-01
                • 2011-10-08
                • 2012-02-02
                • 2023-03-02
                相关资源
                最近更新 更多