繁体   English   中英

带有图像和文本的UIButton不起作用

[英]UIButton with image and text doesn't work

我搜索了解决方案,我在这里看到几个类似的帖子,但我无法找到答案。 我想添加一个设置图像的UIButton(背景颜色也很好)我想在这个图像上添加文字

到目前为止这是我的代码:

        labelSorry.text = @"לא נמצאו תוצאות.";//add text
        //add button.
        Request =[UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *requestimg = [UIImage imageNamed:@"requestcontact_resize2.png"];
        Request.frame = CGRectMake(0.0, 120, 320.0, 30.0);
        Request.titleLabel.textAlignment = NSTextAlignmentRight;
       // Request.titleLabel.textColor = UIColorFromRGB(0xffffff);
        [Request setBackgroundImage:requestimg forState:UIControlStateNormal];
        Request.imageView.image = [UIImage imageNamed:@"requestcontact_resize2.png"];//see no image :( this is kidding me

      //  [Request setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 120, 320.0, 30.0)];///You can replace it with your own dimensions.
        label.textColor = UIColorFromRGB(0xffffff);
        label.text = @"בקש פרטי איש קשר";
        [Request addSubview:label];

        //[Request setBackgroundColor:UIColorFromRGB(0x704c9f)];
        Request.titleLabel.text = @"בקש פרטי איש קשר";
        [Request addTarget:self action:@selector(requestDetails) forControlEvents:UIControlEventTouchUpInside];
        [noResultsView addSubview:Request];
        [self.view addSubview:noResultsView];

我可以看到按钮的背景,但我看不到任何文字:(也许有人可以启发我?

你可以看到我尝试了各种解决方案(我把它们放在评论中),但都没有。

PS我没有使用xib。

任何帮助将受到高度赞赏。

您应该使用[button setImage:forState:][button setTitle:forState:] 如果要为按钮设置背景图像,则应使用[button setBackgroundImage:forState:] 此外,如果需要,您可以更改图像和标题的位置,以便使用[button setImageEdgeInsets:][btn setTitleEdgeInstes:]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM