0

我正在尝试将自定义UIButton放在自定义上,UIViewUIButton没有出现。虽然当我尝试打印UISubViewsmyUIView时,它会在那里显示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];
    }

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

4

6 回答 6

1

我的线索:

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

2.probably它是阿尔法?!

尝试使用:

self.alpha = 1.0;

希望它有效:D

于 2012-10-03T00:02:28.367 回答
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];
    }
于 2012-10-03T00:37:22.200 回答
0

你需要为你的按钮添加一个背景颜色——在你设置它之前它是空的。

于 2012-10-03T00:12:46.650 回答
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];
于 2012-10-03T00:16:01.147 回答
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];
于 2012-10-03T00:51:22.310 回答
-1

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

于 2012-10-03T00:11:59.677 回答