When I try programmatically adding a UISlider to UIView, it wont appear if the width is too high - iphone

I'm trying to programmatically add a vertical UISlider to a UIView, with a variable height. This is what I have so far
int fullSize = [myBottle.barBottem intValue] - [myBottle.barTop intValue];
CGRect frame = CGRectMake(200, 150,fullSize, 23);
slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:#selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor]];
slider.minimumValue = 0.0;
slider.maximumValue = fullSize;
slider.continuous = YES;
slider.transform = CGAffineTransformMakeRotation(M_PI * -
0.5);;
[self.view addSubview:slider];
My problem is that whenever fullSize is > 320, the slider wont appear. Any help would be greatly appreciated!

You can try this
and make slider value method also
UISlider *loslider=[[UISlider alloc]initWithFrame:CGRectMake(60, 80, 200, 30)];
[loslider addTarget:self action:#selector(slider_Action:) forControlEvents:UIControlEventValueChanged];
[loslider setMaximumValue:100.0];
[loslider setMinimumValue:0.0];
[loslider setValue:0.0];
[self.view addSubview:loslider];
then -(void)slider:Action

The UIScrollView's frame should not be bigger than the containing UIView's rect. You should be altering the UIScrollView's contentSize, setting its height/width instead.
UIScrollView * scrollview ... ;
scrollview.contentSize = CGSizeMake(newwidth,newheight);

Try this Code:
1.Create a new slider
-(IBAction)mySlider
{
CGRect frame = CGRectMake(20, 25, 200.0, 10.0);
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor]];
slider.minimumValue = 0.0;
slider.maximumValue = 50.0;
slider.continuous = YES;
slider.value = 25.0;
[self.view addSubview:slider];
}
2.For Slider Actions
-(void)sliderAction:(id)sender
{
float value = [sender floatValue];
NSLog(#"%#",value);
//-- Do further actions
}

Related

Trouble Setting Button Image When On a ScrollView

I'm making an app. My app is based off this sample project. https://github.com/yeahdongcn/RSCircaPageControl
In the app there is a view. On the view, there is a scroll view (UIScrollView *scrollView) which allows to 10 "pages" of content. Within each page, there is another scroll view (UIScrollView *sv) which is a subview of *scrollView.
On SV, I then added a button called UIButton *button. When I attempt to change the button's images after a time of "two minutes," nothing happens. This is because there are 10 buttons. I need to change the image of the button on only one of the pages. Not all of them.
Something tells me it needs "object at index" or something to specify which button on which page I want to change.
The code is below! Thanks guys!
- (void)viewDidLoad
{
[super viewDidLoad];
self.clipView = [[RSView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.clipView.clipsToBounds = YES;
[self.view addSubview:self.clipView];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.clipView.bounds.size.width, kScrollViewHeight)];
self.scrollView.delegate = self;
self.scrollView.clipsToBounds = NO;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.scrollView];
self.clipView.scrollView = self.scrollView;
self.pageControl = [[RSCircaPageControl alloc] initWithNumberOfPages:10];
CGRect frame = self.pageControl.frame;
frame.origin.x = self.view.bounds.size.width - frame.size.width - 10;
frame.origin.y = roundf((self.view.bounds.size.height - frame.size.height) / 2.);
self.pageControl.frame = frame;
self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.pageControl setCurrentPage:0 usingScroller:NO];
[self.view addSubview:self.pageControl];
CGFloat currentY = 0;
for (int i = 0; i < 10; i++) {
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, currentY, self.scrollView.bounds.size.width, kScrollViewHeight)];
sv.tag = kScrollViewTagBase + i;
sv.delegate = self;
sv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
sv.backgroundColor = [UIColor colorWithRed:(arc4random() % 257) / 256. green:(arc4random() % 257) / 256. blue:(arc4random() % 257) / 256. alpha:1];
///here's the buttton!!
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage*normal = [UIImage imageNamed:#"girl.jpg"];
UIImage*selected = [UIImage imageNamed:#"girl2.jpg"];
button.frame = CGRectMake(100, 100, 50,50));
[button setImage:normal forState:UIControlStateNormal];
[button setImage:selected forState:UIControlStateHighlighted];
button.contentMode = UIViewContentModeScaleAspectFit;
[button addTarget:self
action:#selector(myButtonPressed:)
forControlEvents:UIControlEventTouchDown];
[sv addSubview:button]; //notice that the button is added as a subview of SV
[self.scrollView addSubview:sv]; // notice that SV is a subview of the scrollView.
if (i == 2 || i == 6) {
sv.contentSize = CGSizeMake(sv.contentSize.width, kScrollViewContentHeight);
}
currentY += kScrollViewHeight;
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, currentY);
[self performSelector:changeButtomImages withObject:nil afterDelay:TwoMinutes];
}
- (void)changeButtonImages {
UIImage*normal2 = [UIImage imageNamed:#"boy.jpg"];
UIImage*selected2 = [UIImage imageNamed:#"boy2.jpg"];
[button setImage:normal2 forState:UIControlStateNormal];
[button setImage:selected2 forState:UIControlStateHighlighted];
}
Kyle
for setting button image in a normal state the parameter to pass forState: is UIControlStateNormal not UIControlStateHighlighted
So, in changeButtonImages you are changing the the image for the iVar "button", but you are never actually assigning anything to that button var. In your for loop in viewDidLoad, you are creating a local variable of UIButton called button. Maybe something like this?
- (void)myButtonPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
UIImage*normal2 = [UIImage imageNamed:#"boy.jpg"];
UIImage*selected2 = [UIImage imageNamed:#"boy2.jpg"];
[button setImage:normal2 forState:UIControlStateNormal];
[button setImage:selected2 forState:UIControlStateHighlighted];
}

Add Button on my View

I'm creating a hidden form containing 3 buttons at the bottom of the parent view, by pressing another button, the view becomes visible, but those 3 buttons are not pressed, I don `t know what the problem is.
I add a button via code. Here is a method that is called when the button is pressed.
- (IBAction) addData:(id)sender
{
if(attachInfo==nil){
float screenHeight = self.view.frame.size.height;
attachInfo=[[UIView alloc] initWithFrame:CGRectMake(0, screenHeight, 320, 216)];
UIButton *attachImage = [UIButton buttonWithType:UIButtonTypeCustom];
//attachImage.enabled = YES;
[attachImage addTarget:self action:#selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
[attachImage setFrame:CGRectMake(20, 20, 0, 0)];
[attachImage setImage:[UIImage imageNamed:#"add_photos"] forState:UIControlStateNormal];
[attachImage sizeToFit];
[attachInfo addSubview:attachImage];
UIButton *attachDoc = [UIButton buttonWithType:UIButtonTypeCustom];
//[pushButton addTarget:self action:#selector(pushViewController) forControlEvents:UIControlEventTouchUpInside];
[attachDoc setFrame:CGRectMake(120, 20, 80, 80)];
[attachDoc setImage:[UIImage imageNamed:#"add_docs.png"] forState:UIControlStateNormal];
[attachDoc sizeToFit];
[attachInfo addSubview:attachDoc];
UIButton *attachPlace = [UIButton buttonWithType:UIButtonTypeCustom];
//[pushButton addTarget:self action:#selector(pushViewController) forControlEvents:UIControlEventTouchUpInside];
[attachPlace setFrame:CGRectMake(220, 20, 80, 80)];
[attachPlace setImage:[UIImage imageNamed:#"add_place.png"] forState:UIControlStateNormal];
[attachPlace sizeToFit];
[attachInfo addSubview:attachPlace];
//[self.view addSubview:attachInfo];
[self.view addSubview:attachInfo];
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y -= 216;
attachInfo.superview.frame=frame;
}];
}
else
{
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y += 216;
attachInfo.superview.frame=frame;
}];
attachInfo=nil;
}
}
you need to add Action of buttons.
- (IBAction)first:(id)sender;
{
firstViewController * fvc=[FirstViewController alloc]init];
[FirstButton setHidden:False];
[self.navigationController pushViewController:fvc animated:YES];
}
- (IBAction)Second:(id)sender;
{
SecondViewController * svc=[SecondViewController alloc]init];
[SecondButton setHidden:False];
[self.navigationController pushViewController:svc animated:YES];
}
- (IBAction)Third:(id)sender;
{
ThirdViewController * tvc=[ThirdViewController alloc]init];
[ThirdButton setHidden:False];
[self.navigationController pushViewController:fvc animated:YES];
}
In the first button (attachImage), the height and the width is zero. So it should not be visible. For the other two buttons you haven't set the target. So those are not clickable.
for the first button set
[attachImage setFrame:CGRectMake(20, 20, 80, 80)];
and for the other two buttons add
//for the second button
[attachDoc addTarget:self action:#selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
//for the third button
[attachPlace addTarget:self action:#selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
-(void)createHiddenForm
{
hiddenView = [[UIView alloc] initWithFrame:CGRectMake(0,100,320,300)];
[hidden setHidden:TRUE];
[self.view addSubview:hiddenView];
float posY = 50.0;
for (int i=0 ; i<3 ; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(50,posY, 100, 50)];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[hiddenView addSubview:button];
posY=posY+60;
}
}
-(void)showHiddenView
{
hidden.hidden = FALSE;
[self.view bringSubviewToFront:hiddenView];
}
-(void)buttonClicked:(id)sender
{
UIButton *senderBtn = (UIButton *)sender;
NSLog(#"Sender Button's tag = %#",senderBtn.tag);
}
Hope it will help you.
float screenHeight = self.view.frame.size.height;
attachInfo=[[UIView alloc] initWithFrame:CGRectMake(0, screenHeight, 320, 216)];
From the above, u r adding the attachInfo Y position outside the view boundaries.
Button onclick won't work when added outside the view.
To make it work replace below line.
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y -= 216;
attachInfo.superview.frame=frame;
Instead of moving superview frame move attachInfo frame like this.
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.frame;
frame.origin.y -= 216;
attachInfo.frame=frame;
So that attachInfo view will be added to the super view boundaries with animation.

Strange Behaviour of UIScrollview

I need to draw dynamic images on UIScrollView for this i use, MKHorizion menu (A subclass of Scroll view). Now i add Subview images on scrollview. Loop worked perfectly for adding subview on scrollview. Now in My parent class I need to touch scroll view for updated data. If i didn't touch then it is not showing updated data. Below is code
-(void) reloadData
{
[self deleteAlliTem];
self.itemCount = [dataSource numberOfImagesForMenu:self];
self.backgroundColor = [dataSource backgroundColorForImage:self];
UIFont *buttonFont = [UIFont boldSystemFontOfSize:15];
int buttonPadding = 0;
int tag = kButtonBaseTag;
int xPos = kLeftOffset;
for(int i = 0 ; i < self.itemCount; i ++)
{
NSLog(#"*************************************************************");
NSMutableDictionary *dictData=[dataSource horizMenuImage:self dictForItmeAtIndex:i];
NSString *title=[dictData valueForKey:#"name"] ? [dictData valueForKey:#"name"] : #"";
UIImage* imageItem= [UIImage imageWithData:[Base64 decode:[dictData valueForKey:#"img"]]];
int buttonWidth = 95;
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setTitle:title forState:UIControlStateNormal];
customButton.titleLabel.font = buttonFont;
[customButton setBackgroundImage:[UIImage imageNamed:#"addFriendStrip.png"] forState:UIControlStateNormal];
[customButton setBackgroundImage:[UIImage imageNamed:#"addFriendStrip.png"] forState:UIControlStateSelected];
customButton.tag = tag++;
[customButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
customButton.frame = CGRectMake(xPos, 70, buttonWidth + buttonPadding, 25);
customButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
customButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
customButton.titleEdgeInsets =UIEdgeInsetsMake(0, 10,0, 0);
customButton.titleLabel.font = [UIFont fontWithName:#"Overlock-Bold" size:16];
[customButton setTitleColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
[customButton setTitleColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateSelected];
UIImageView* itemImageView = [[UIImageView alloc] initWithImage:imageItem];
itemImageView.tag = 200 + customButton.tag;
[itemImageView setFrame:CGRectMake(xPos, 0, buttonWidth + buttonPadding, 95)];
[self addSubview:itemImageView];
[itemImageView release];
itemImageView = nil;
[self addSubview:customButton];
xPos += buttonWidth;
xPos += buttonPadding;
if (i != self.itemCount-1){
xPos += 2.5; //5; // Richa
}
}
self.contentSize = CGSizeMake(xPos, 95);
NSLog(#"############################################################################ - %d",[[self subviews] count]);
[self scrollRectToVisible:CGRectMake(1, 0, self.frame.size.width, self.frame.size.height) animated:YES];
}
Please Help me to sort out this. Why do i needed to touch scroll view ? Do i need to override other methods ?
Did you check it is on main thread ? may be you are using GCD Queue thats why it is not updating till touch.
just try to bring your scrollview on top.I am not getting your question correctly ,but it may help.
Let me know if it is working or not..!!!
Happy Coding.!!!!

UIButton selector not working on mainwindow view

I am adding a custom view on main window then on the view i am creating a button and button is showing but the action is not performing on button tapped.
on a button tap i want to open a photogallery.
and in a loop the buttons are created.
code that i am using is there...
UIImage *image=[[UIImage alloc] initWithData:data cache:NO];
UIImageView *iv=[[UIImageView alloc] initWithFrame:CGRectMake(200, y, 75, 75)];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, y, 75, 75)];
[button setAlpha:0];
[button setTag:i];//change this to your loop counter
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
iv.image=image;
// iv.tag=#"bigimage.jpg";
iv.userInteractionEnabled=YES;
button.userInteractionEnabled = YES;
[iv addSubview:button];
[button release];
y=y+145;
[view1 addSubview:iv];
[iv release];
And also try this code but no improvements are there.....
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(200, y, 75, 75);
UIImage *img = [[UIImage alloc] initWithData:data];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
y = y+145;
[view1 addSubview:button];
The frame of your button should not have the same origin x and y as your imageview. If you want it inside the image view it should have origin x:0 y:0 and the same width and height as your imageview.
The origin x,y is relative to it's parent. In your case the imageview.
Hello Shivangi,
I think you want to set image on button so try this code its will help you
//function to create button dynamically on scrollview
-(void)createButton
{
int Y = 20;
indexRow = 0;
for (int i = 0; i < [prow.noOfPhotos count]; i++)
{
thumbNailButton = [UIButton buttonWithType:UIButtonTypeCustom];
int X = 4 + (78 * (i % 4));
NSLog(#"X = %d",X);
NSLog(#"Start Y = %d",Y);
if (i % 4 == 0)
{
NSLog(#"%d",i);
Y = (70 * verticalImageRowCount);
NSLog(#"Y = %d",Y);
verticalImageRowCount++;
NSLog(#"VerticalImageRowCount= %d");
}
CGRect rect = myScrollView.frame;
rect.size.width = 100;
rect.size.height = Y + 77;
myScrollView.contentSize = CGSizeMake(rect.size.width,rect.size.height);
thumbNailButton.frame = CGRectMake(X,Y, 62,65);
view = [[UIImageView alloc] init];
prow.photo = [prow.noOfPhotos objectAtIndex:indexRow];
imagePath = prow.photo;
asyncImageView = [[[AsyncImageView alloc] initWithFrame:CGRectMake(4, 0, 62, 65)] autorelease];
asyncImageView.backgroundColor = [UIColor clearColor];
[view addSubview:asyncImageView];
NSURL *url = [NSURL URLWithString:imagePath];
[asyncImageView loadImageFromURL:url];
[view setImage:[UIImage imageNamed:[prow.noOfPhotos objectAtIndex:i]]];
thumbNailButton.tag = i;
thumbNailButton.backgroundColor=[UIColor clearColor];
[thumbNailButton addTarget:self action:#selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
[thumbNailButton addSubview:view];
[myScrollView addSubview:thumbNailButton];
indexRow+= 1;
}
}
Try putting an NSLog in your buttonPressed: and see if that method is getting called. Maybe the method is getting called but the Photo Gallery app isn't launching for some other reason.
The user cannot click the button if you set the button's alpha to 0. This means the event will not be fired.
[button setAlpha:0];
This has the same effect as setting button.hidden = YES.
If you want an invisible clickzone using the button, set the button type to UIButtonTypeCustom and it should do the trick.

How can I create and display a UISlider programmatically in iOS?

I found the code below in the Apple documentation and added it to my viewDidLoad method but the slider doesn't appear when I run the code.
CGRect frame = CGRectMake(0.0, 0.0, 200.0, 10.0);
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor]];
slider.minimumValue = 0.0;
slider.maximumValue = 50.0;
slider.continuous = YES;
slider.value = 25.0;
You need to add the slider into your view hierarchy. Add [self.view addSubview:slider]; and it should work.
Try this Code:
Create a new slider
-(IBAction)mySlider
{
CGRect frame = CGRectMake(0.0, 0.0, 200.0, 10.0);
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor]];
slider.minimumValue = 0.0;
slider.maximumValue = 50.0;
slider.continuous = YES;
slider.value = 25.0;
[self.view addSubview:slider];
}
Slider Actions
-(void)sliderAction:(id)sender
{
UISlider *slider = (UISlider*)sender;
float value = slider.value;
//-- Do further actions
}
Adding to the view via [self.view addSubview:slider]
AS PREVIOUS MENTIONED - is essential.
wasn't visible FOR ME at :
CGRect frame = CGRectMake(0.0, 0.0, 200.0, 10.0);
You should probably drop it down a bit and maybe get it off the left margin.
perhaps?
CGRect frame = CGRectMake(5.0, 10.0, 200.0, 10.0);
For ios 6 and later you should really add this to view did load as well
(so you can see it - even if you lowered the frame)
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
{
self.edgesForExtendedLayout=NO;
}