显示自定义UIButton时出现问题

时间:2012-10-02 23:57:48

标签: iphone ios cocoa-touch uiview uibutton

我正在尝试将自定义UIButton放在自定义UIView上,但UIButton未显示。虽然当我尝试打印UISubViews的{​​{1}}时,它会在那里显示UIView个对象。

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

UIView

有人知道我做错了吗?

6 个答案:

答案 0 :(得分:1)

我的线索:

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

2.可能是 alpha ?!

尝试使用:

self.alpha = 1.0;

希望它有效:D

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

也许这个:

 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];

答案 3 :(得分:0)

在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];
    }

答案 4 :(得分:0)

你的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];

答案 5 :(得分:-1)

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