显示自定义 UIButton 时出现问题



我正在尝试将自定义UIButton放在自定义UIView上,但UIButton没有出现。虽然当我尝试打印UISubViews我的UIView时,它在那里显示了UIButton对象。

下面是我正在编写的代码片段,用于将按钮放在我的自定义UIView

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        self.headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.headerLabel.backgroundColor = [UIColor clearColor];
        self.headerLabel.textColor = [UIColor whiteColor];
        self.headerLabel.font = [UIFont systemFontOfSize:kApplicationHeaderTextFont];
        [self addSubview:self.headerLabel];
        self.actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.actionButton.frame = CGRectZero;
        [self addSubview:self.actionButton];
    }
    return self;
}
- (void)drawRect:(CGRect)iTotalRect{
    if (self.actionButtonImage) {
        self.actionButton.frame = CGRectMake(self.frame.size.width - self.actionButtonImage.size.width - 10.0, self.frame.size.height / 2 - self.actionButtonImage.size.height / 2, self.actionButtonImage.size.width, self.actionButtonImage.size.height);
        [self.actionButton setImage:self.actionButtonImage forState:UIControlEventTouchUpInside];
    }

有人知道我做错了什么吗?

我的线索:

1.你为什么不使用故事板?

2.可能是阿尔法?!

尝试使用:

self.alpha = 1.0;

希望它:D有效

所以我找到了你需要的东西!!

也许这个:

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

现在您可以添加您的选项了!试试吧!

这是我的应用程序的一部分,您可以理解我的意思:

- (void)addMyButton{    // Method for creating button, with background image and other properties
    UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
    [playButton setTitle:@"Play" forState:UIControlStateNormal];
    playButton.backgroundColor = [UIColor clearColor];
    [playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
    UIImage *buttonImageNormal = [UIImage imageNamed:@"blueButton.png"];
    UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
    UIImage *buttonImagePressed = [UIImage imageNamed:@"whiteButton.png"];
    UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
    [playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playButton];  
}

好吧,我为您找到了最终解决方案:

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

您需要为按钮添加背景色 - 在设置之前为空。

drawRect中的以下代码对我有用。因此,基本上使用自定义按钮,您不应该设置图像,而应该设置背景图像。

- (void)drawRect:(CGRect)iTotalRect
{
    if (self.actionButtonImage) {
        self.actionButton.frame = CGRectMake(self.frame.size.width - self.actionButtonImage.size.width - 10.0, self.frame.size.height / 2 - self.actionButtonImage.size.height / 2, self.actionButtonImage.size.width, self.actionButtonImage.size.height);
        [self.actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.actionButton setBackgroundImage:self.actionButtonImage forState:UIControlStateNormal];
    }
<</div> div class="one_answers">

你的 actionButton 的 initWithFrame 在哪里?

抱歉,我懒得浏览我的代码,但这是您如何创建自定义按钮的互联网示例。

UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[playButton setTitle:@"Play" forState:UIControlStateNormal];
playButton.backgroundColor = [UIColor clearColor];
[playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:@"blueButton.png"];
UIImage *strechableButtonImageNormal = 
 [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:@"whiteButton.png"];
UIImage *strechableButtonImagePressed = 
[buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton addTarget:self 
action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
可能不会

调用-initWithFrame:,具体取决于您创建按钮的方式。 如果您在 .xib 文件中创建按钮,则可能会调用-initWithCoder:

最新更新