How do I create a UISlider dynamically in code? - iphone

I would like to know how to create a UISlider at runtime.

You can add a slider like ever other view:
UISlider *slider = [[[UISlider alloc] initWithFrame:CGRectMake(50, 50, 100, 40)] autorelease];
[slider addTarget:self action:#selector(controlValueChanged:) forControlEvents:UIControlEventValueChanged];
slider.minimumValue = 100.00;
slider.maximumValue = 1000.00;
slider.continuous = YES;
[self.view addSubview:slider];
Use a frame that suits your needs, and add to the appropriate view (I used self.view in this example). Feel free to configure as needed. ;-)

CGRect frame = CGRectMake(50, 170, 205, kSliderHeight);
customSlider = [[UISlider alloc] initWithFrame:frame];
[customSlider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
customSlider.backgroundColor = [UIColor clearColor];
UIImage *stetchLeftTrack = [[UIImage imageNamed:#"yellowslide.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:#"orangeslide.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
[customSlider setThumbImage: [UIImage imageNamed:#"slider_ball.png"] forState:UIControlStateNormal];
[customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];

Related

How to display two buttons in popoverview - WEPOPOVER

I want to display two buttons inside the popoverview. I am using WEPOPOVER, I am able to display one btn.
But I am not sure how to display the two buttons.
UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[editBtn setTitle:#"EDIT ME" forState:UIControlStateNormal];
[editBtn setFrame:CGRectMake(250, 0, 100,40)];
[editBtn addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
[editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
editBtn.titleLabel.shadowOffset = CGSizeMake(0, -1);
editBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
editBtn.backgroundColor = [UIColor blackColor];
UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[deleteBtn setTitle:#"Delete ME" forState:UIControlStateNormal];
[deleteBtn setFrame:CGRectMake(250, 0, 100,40)];
[deleteBtn addTarget:self action:#selector(doSubscription) forControlEvents:UIControlEventTouchUpInside];
[deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
deleteBtn.titleLabel.shadowOffset = CGSizeMake(0, -1);
deleteBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
deleteBtn.backgroundColor = [UIColor blackColor];
// place inside a temporary view controller and add to popover
UIViewController *viewCon = [[UIViewController alloc] init];
viewCon.view = (UIView *)[NSArray arrayWithObjects:editBtn,deleteBtn, nil];
viewCon.contentSizeForViewInPopover = editBtn.frame.size; // Set the content size
navPopover = [[WEPopoverController alloc] initWithContentViewController:viewCon];
[navPopover presentPopoverFromRect:editButton.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown
animated:YES];
You have given the same frame for both buttons. So it shows just one button.
[editBtn setFrame:CGRectMake(250, 0, 100,40)];
[deleteBtn setFrame:CGRectMake(250, 0, 100,40)];

How do I create a simple button without using the Interface builder?

How do I create a simple button without using the Interface builder?
You would need to declare a UIButton like this:
UIButton *newButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
[newButton setTitle:#"Some Button" forState:UIControlStateNormal];
You would then add the button to a view -
[self.view addSubview: newButton]; // (could be another view other then self.view)
And finally, if you want some action to occur when pressing that button, you would add:
[newButton addTarget:self action:#selector(doSomething)
forControlEvents:UIControlEventTouchUpInside];
and of course declare that function:
- (void) doSomething
{
NSLog(#"Button pressed");
}
And of course don't forget to release the button.
Try with below
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
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];
like this-
custom button -
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 30.0f)];
system buttom -
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

how can I set actions on UIImages when I am using them as buttons?

Hello
I am using images as buttons so how can I put event methods as they pressed, my coding is as below ...
UIImageView* view = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"front_pag.png"]];
view.frame = CGRectMake(0, 0, 320, 460);
[self.view addSubview:view];
new_button = [[UIButton alloc] init];
UIImage *img1 = [UIImage imageNamed:#"new.png"];
[new_button setImage:img1 forState:UIControlStateNormal];
[new_button setFrame:CGRectMake(85, 430, 0, 0)];
[new_button addTarget:self action:#selector(newMethod)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:new_button];
what should I do, because in this case, I can't call newMethod, even I have this method in same class.
what should I do ?
You should change your addTarget call to this:
[new_button addTarget:self action:#selector(newMethod:)
forControlEvents:UIControlEventTouchUpInside];
Pay attention to semicolon after the newMethod: name
This is because the signature of your selector should be as following:
- (void) newMethod:(id) sender
This way it will be called.
Good luck
I think you can try this out,
uiimage *img1 = [UIImage imageNamed:#"someImage.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setImage:img1 forState:UIControlStateNormal]
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

combining UISegmentedControl and UIBarButtonItem in navigation controller to customized barbuttonitem background image

I have a barbuttonitem that made programmatically above a navigation controller. I want to display it's title and background also a highlighted effect when i press that bar button.
Here's the code I use :
NSArray *segmentText = [segmentTextMutable copy];
UIImage *image = [[[UIImage alloc] init] autorelease];
image = [UIImage imageNamed:#"bunga.jpg"];
_docSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentText];
_docSegmentedControl.selectedSegmentIndex = 0;
_docSegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
_docSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
[_docSegmentedControl addTarget:self action:#selector(docSegmentAction:) forControlEvents:UIControlEventValueChanged];
[_docSegmentedControl setBackgroundColor:[UIColor colorWithPatternImage:image]];
UIView *barBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"button.png"];
UIImage *buttonPressedImage = [UIImage imageNamed:#"buttonPressed.png"];
[[barButton titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0]];
[barButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[barButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[barButton setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.7] forState:UIControlStateNormal];
[barButton setTitleShadowColor:[UIColor clearColor] forState:UIControlStateHighlighted];
[[barButton titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)];
CGRect buttonFrame = CGRectMake(0, 0, 110.0, 40);
//buttonFrame.size.width = 110.0;
//buttonFrame.size.height = buttonFrame.size.height;
[barButton setFrame:buttonFrame];
[barButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[barButton setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];
[barButton setTitle:docSegmentFileName forState:UIControlStateNormal];
[barButton addTarget:self action:#selector(docSegmentAction:) forControlEvents:UIControlEventTouchUpInside];
[barBackground addSubview:barButton];
UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:_docSegmentedControl];
self.navItem.leftBarButtonItem = segmentItem;
self.navItem.leftBarButtonItem.title = #"";
[self.navItem.leftBarButtonItem setCustomView:barBackground];
Unfortunately, this doesn't work. Instead of showing the UIBarButtonItem, it simply vanishes (it becomes 100% transparant). When I leave out the setCustomView method, the UIBarButtonItem appears, but is not customized. How can i solve this problem?
thank's...
You are removing the UISegmentedControl from the UIBarButtonItem when you call [self.navItem.leftBarButtonItem setCustomView:barBackground];. You need to modify the UISegmentedControl not the UIBarButtonItem.

uibutton+setselected

i have implemented the following code in my project!
UIImage *add_item_icon = [UIImage imageNamed:#"add_item_icon.png"];
UIImage *img = [UIImage imageNamed:#"add_button.png"];
if(headerButton != nil)
[headerButton release];
headerButton = [[UIButton alloc] init];
headerButton.frame = CGRectMake(0, 0, 320, 42);
headerButton.backgroundColor = [UIColor clearColor];
headerButton.showsTouchWhenHighlighted = YES;
[headerButton setBackgroundImage:img forState:UIControlStateNormal];
[headerButton setTitle:#"Add New Gallery" forState:UIControlStateNormal];
[headerButton setImage:add_item_icon forState:UIControlStateNormal];
headerButton.titleLabel.font = [UIFont systemFontOfSize:16];
headerButton.titleLabel.textColor = [UIColor darkGrayColor];
[headerButton addTarget:self action:#selector(headerButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//[headerButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateSelected];
My question is whenever i click the button, the color which is assigned to the title has been changed. The requirement is it should not be like that. Any one pls guide me! as early possible.
Not sure what is in your -headerButtonClicked:, but you should remove anything that looks like this in -headerButtonClicked:
headerButton.titleLabel.textColor = [UIColor darkGrayColor];