dubbeat
dubbeat

Reputation: 7857

can I add a uiprogressbar to a uibutton

I want to know if it is possible to subclass UIButton someway to add another component to it such as a preloader or progress indicator.

In pseudo code what I have in mind is something like the following.

UIButtonWithProgress *button =[[UIButtonWithProgress alloc]init]

[button.progressbar setProgress:50%]

Upvotes: 0

Views: 1577

Answers (1)

James Bedford
James Bedford

Reputation: 28982

In iOS the class for a progress bar is UIProgressView. If you really wanted to add a UIButton to a UIProgressBar (as in the question title). Then you can use the UIView method addSubView: For example:

UIButtonWithProgress* button = [[UIButtonWithProgress alloc] init];
UIProgressView* progressView = [[UIProgressView alloc] init];
[button addSubview:progressView];
progressView.progress = 0.5;

I think what you really want to do is create your own UIControl (so you should subclass that instead). You can override the method drawRect: to use quartz to draw your own progress bar animation, and the touchesBegan:withEvent: to detect the user tapping on your custom control, as well as adding any other methods you wish to control the state of the control (such as the progress level).

Note: In my code example I also give an example of correctly setting the progress of the UIProgressBar, just in case you were unclear.

Upvotes: 3

Related Questions