Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

iphone - Making a custom Button using a UIView or overriding UIButton?

I need to generate a custom button through code, this is how i am currently doing it.

-(void) initialiseButtons 
{
    int ypos = playerImage.frame.origin.y + playerImage.frame.size.height + 8;
    for(int i=0; i<totalButtons; i++) 
    {       
        UIButton *newButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
        [newButton setFrame:CGRectMake(20, ypos, 220, 30)];
        newButton.tag = 10 + i;
        [newButton addTarget:self action:@selector(statTapped:) forControlEvents:UIControlEventTouchUpInside];
        [self.frontView addSubview:newButton];
        ypos += 30 + 7;
    }
}

This creates the blank buttons perfectly through code, gives them a tag and assigns an callback function on touchUpInside.

The custom button needs to be able to handle showing an image when pressed down. It needs to be able to draw 2 pieces of Text. 1 aligned to the left hand side of the button and 1 aligned to the righthand side of the button.

My boss suggested instead of buttons I use a View. I dont understand how this will work. When i start thinking about it, i think it would require having a viewcontroller dedicated to the buttons. And some draw method? It sounds complicated and I am not grasping how it can be done.

Is there a simpler method by making a custom class overriding UIButton? I made a simple test class earlier but nothing was drawn in the buttons locations when I used them in place of the Normal UIButton class. I expected i would see the buttonUp.png drawn.

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface CardButton : UIButton {
}
@end


#import "CardButton.h"
#import <UIKit/UIKit.h>
@implementation CardButton
- (id)initWithFrame:(CGRect)frame 
{
    if (self = [super initWithFrame:frame]) 
    {
        [self setImage:[UIImage imageNamed:@"buttonUp.png"] forStates:UIControlStateNormal];
        self.adjustsImageWhenHighlighted = YES;
    }
    return self;
}
@end

Anybody able to give me some pointers on this? I'm pretty blocked at the moment. Have been reading some various other threads related to buttons but nothing that made it clear in my head how to tackle the problem

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Personally, I would subclass UIButton. Despite all the talk that you can't subclass UIButton, Apple even talks about subclassing UIButton in the UIButton documentation.

In the subclass I would create a UIView with the two labels (or images or whatever) and add them as subviews to the button (be sure to set interactive for the text and view as FALSE).

What is awesome about this is that it leverages the UIButton code and keeps you from reinventing the wheel.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...