带图片和文字的UIButton [英] UIButton with Picture and Text

查看:294
本文介绍了带图片和文字的UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个基本上模仿iphone主屏幕上看到的按钮的按钮 - 它有一张垂直放置在某些文字上方的图片,两者都是按钮的一部分,表现得恰到好处(昏暗,可点击等)突出显示时。

I'm trying to make a button that basically mimics the buttons seen on the iphone's home screen - it has a picture placed vertically above some text, and both are part of the button and behave appropriately (dim, clickable, etc) when highlighted.

我已尝试将图片或文字放入自己的框架中,但这不起作用,因为新框架中的任何一个都不会与其余部分一起调暗按钮。

I've tried putting the picture or the text into its own frame but that doesn't work because whichever is in the new frame doesn't dim with the rest of the button.

使用背景图像属性不起作用,因为我希望图像位于文本上方 - 文本后面的区域应该是透明的。我真的更喜欢编程解决方案,而不是进入和编辑我的所有图片。

Using the background image property doesn't work because I want the image above the text - the area behind the text should be transparent. I would REALLY prefer a programming solution instead of going in and editing all my pictures.

推荐答案

创建UIView的子类,并将UIImageView和UILabel作为子视图,并使用代码正确定位它们。

Create a subclass of UIView, and have a UIImageView and UILabel as subviews, and position them appropriately using code.

然后你将UIImageView和UILabel作为属性,以便你可以访问它们,如下所示:

You'd then have the UIImageView and UILabel as properties so that you could access them, like this:

myCustomButton.imageView.image = [UIImage imageNamed:@"..."];
myCustomButton.textLabel.text = @"...";

或者你可以在github上的互联网上找到第三方自定义UIView。

Or you could probably find a 3rd Party custom UIView, on the internet on github or something.

这篇关于带图片和文字的UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆