0

カスタムにカスタムを配置しようとして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];
    }

誰かが私が間違っていることを知っていますか?

4

6 に答える 6

1

私の手がかり:

1.ストーリーボードを使用しないのはなぜですか?

2.おそらくそれはアルファですか?!

使用してみてください:

self.alpha = 1.0;

それがうまくいったことを願っています:D

于 2012-10-03T00:02:28.367 に答える
0

ボタンに背景色を追加する必要があります。設定するまではnullです。

于 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

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

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 に答える