سؤال

كيف يمكنك تعيين الصورة لأوزيبتون في الكود؟

لدي هذا:

UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];
[btnTwo addTarget:self action:@selector(goToOne) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnTwo];

ولكن لا ترى ما سيقوم بتعيين الصورة لذلك.

هل كانت مفيدة؟

المحلول

ج موضوعية

UIImage *btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setImage:btnImage forState:UIControlStateNormal];

SWIFT 4.

let btnImage = UIImage(named: "image")
btnTwo.setImage(btnImage , for: UIControlState.normal)

نصائح أخرى

سيظهر حل مايك فقط الصورة، لكن أي عنوان يتم تعيينه على الزر لن يكون مرئيا، لأنه يمكنك إما تعيين العنوان أو الصورة.

إذا كنت ترغب في ضبط كل من (صورتك وعنوانك)، فاستخدم التعليمات البرمجية التالية:

btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnTwo setTitle:@"Title" forState:UIControlStateNormal];

قبل ذلك سيعمل من أجلي، اضطررت إلى تغيير حجم إطار الزر بناء على حجم إطار الصورة.

UIImage *listImage = [UIImage imageNamed:@"list_icon.png"];
UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];

// get the image size and apply it to the button frame
CGRect listButtonFrame = listButton.frame;
listButtonFrame.size = listImage.size;
listButton.frame = listButtonFrame;

[listButton setImage:listImage forState:UIControlStateNormal];
[listButton addTarget:self.navigationController.parentViewController 
               action:@selector(revealToggle:) 
     forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton = 
  [[UIBarButtonItem alloc] initWithCustomView:listButton];

self.navigationItem.leftBarButtonItem = jobsButton;

في حالة مستخدم سويفت

// case of normal image
let image1 = UIImage(named: "your_image_file_name_without_extension")!
button1.setImage(image1, forState: UIControlState.Normal) 

// in case you don't want image to change when "clicked", you can leave code below
// case of when button is clicked
let image2 = UIImage(named: "image_clicked")!
button1.setImage(image2, forState: UIControlState.Highlight) 

يمكنك أن تفعل ذلك مثل هذا

[btnTwo setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 150, 44);
[btn setBackgroundImage:[UIImage imageNamed:@"buttonimage.png"] 
               forState:UIControlStateNormal];
[btn addTarget:self 
     action:@selector(btnSendComment_pressed:) 
     forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

يمكنك وضع الصورة في أي من الطريق:

UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];
[btnTwo addTarget:self 
           action:@selector(goToOne) 
 forControlEvents:UIControlEventTouchUpInside];


[btnTwo setImage:[UIImage imageNamed:@"name.png"] forState:UIControlStateNormal];

//OR setting as background image

[btnTwo setBackgroundImage:[UIImage imageNamed:@"name.png"] 
                  forState:UIControlStateNormal];

[self.view addSubview:btnTwo];

كنت أبحث عن حل لإضافة UIImage لي UIButton. وبعد كانت المشكلة مجرد عرض الصورة أكبر من اللازم. فقط ساعدني في هذا:

_imageViewBackground = [[UIImageView alloc] initWithFrame:rectImageView];
_imageViewBackground.image = [UIImage imageNamed:@"gradientBackgroundPlain"];
[self addSubview:_imageViewBackground];
[self insertSubview:_imageViewBackground belowSubview:self.label];
_imageViewBackground.hidden = YES;

في كل مرة أريد عرضي UIImageView أنا فقط ضبط var hidden ل YES أو NO. وبعد قد يكون هناك حلول أخرى لكنني حصلت على حيرة من عدة مرات مع هذه الأشياء وحل هذا وحلها ولم أكن بحاجة للتعامل مع الأشياء الداخلية UIButton يفعل في الخلفية.

لا تقلق كثيرا تأطير الزر من التعليمات البرمجية، يمكنك القيام بذلك على لوحة العمل. هذا يعمل بالنسبة لي، خط واحد ... أكثر بسيطة.

[self.button setBackgroundImage:[UIImage imageNamed: @"yourPic.png"] forState:UIControlStateNormal];
-(void)buttonTouched:(id)sender
{
    UIButton *btn = (UIButton *)sender;

    if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"icon-Locked.png"]])
    {
        [btn setImage:[UIImage imageNamed:@"icon-Unlocked.png"] forState:UIControlStateNormal];
        // other statements....
    }
    else
    {
        [btn setImage:[UIImage imageNamed:@"icon-Locked.png"] forState:UIControlStateNormal];
        // other statements....
    }
}

SWIFT 3 الإصدار (يجب أن يكون butt_img صورة تم تعيينها assets.xcassets. أو images.xcassets. مجلد في Xcode):

btnTwo.setBackgroundImage(UIImage(named: "butt_img"), for: .normal)
btnTwo.setTitle("My title", for: .normal)

على أي حال، إذا كنت تريد تحجيم الصورة لملء حجم الزر، يمكنك إضافة UIImageView أكثر من ذلك وتعيينه صورتك:

let img = UIImageView()
img.frame = btnTwo.frame
img.contentMode = .scaleAspectFill
img.clipsToBounds = true
img.image = UIImage(named: "butt_img")
btnTwo.addSubview(img)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top