How can i add an uiscrollview on a navigation bar? [duplicate] - iphone

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can i add more than 10 buttons on a navigationbar in iphone application development?
I want to set an UIScrollView on a navigation bar which containing eight buttons and this navigation bar also containing leftbaritem and rightbaritem when i will press the next or previous button then scrollview have to scroll with this button because some of button will be out of the scrollview frame.
Until now i have made scroll view and buttons but problem is button are now displaying.Can anybody help me by giving an example or sending source code.
Thanks in Advance
EDIT
topToolBar = [UIToolbar new];
[topToolBar sizeToFit];
topToolBar.frame = CGRectMake(0,0, 280, 50);
topToolBar.barStyle = UIBarStyleBlackTranslucent;
segmentedControllerView = [[UIView alloc] initWithFrame:CGRectMake(5, 0, 280, 50)];
segmentedControllerView.clipsToBounds = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0,0, 60.0, segmentedControllerView.frame.size.height -10);
button.clipsToBounds = YES;
[segmentedControllerView addSubview:button];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 addTarget:self
action:#selector(aMethod2:)
forControlEvents:UIControlEventTouchDown];
[button2 setTitle:#"Show View2" forState:UIControlStateNormal];
button2.frame = CGRectMake(60,0, 60.0, segmentedControllerView.frame.size.height - 10);
[segmentedControllerView addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button3 addTarget:self
action:#selector(aMethod3:)
forControlEvents:UIControlEventTouchDown];
[button3 setTitle:#"Show View" forState:UIControlStateNormal];
button3.frame = CGRectMake(120,0, 60.0, segmentedControllerView.frame.size.height -10 );
[segmentedControllerView addSubview:button3];
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button4 addTarget:self
action:#selector(aMethod4:)
forControlEvents:UIControlEventTouchDown];
[button4 setTitle:#"Show View" forState:UIControlStateNormal];
button4.frame = CGRectMake(180,0, 60.0, segmentedControllerView.frame.size.height -10 );
[segmentedControllerView addSubview:button4];
[topToolBar addSubview:segmentedControllerView];
self.navigationItem.titleView = topToolBar;
I am also close to the result but problem is when i am pressing the next or prev button then it moving over the next or prev button but it should be move under the view....
This is the animation code:
-(void)previousBarButtonAction{
[UIView beginAnimations: #"moveField"context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
segmentedControllerView.frame = CGRectMake(segmentedControllerView.frame.origin.x - 50,
segmentedControllerView.frame.origin.y,
segmentedControllerView.frame.size.width,
segmentedControllerView.frame.size.height);
[UIView commitAnimations];
}
-(void)nextBarButtonAction{
[UIView beginAnimations: #"moveField"context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
segmentedControllerView.frame = CGRectMake(segmentedControllerView.frame.origin.x + 50,
segmentedControllerView.frame.origin.y,
segmentedControllerView.frame.size.width,
segmentedControllerView.frame.size.height);
[UIView commitAnimations];
}

I have solve this problem slightly different way....
In viewDidLoad I have take a scrollView and called my function
menuScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,40)];
menuScrollView.showsHorizontalScrollIndicator = FALSE;
menuScrollView.showsVerticalScrollIndicator = FALSE;
menuScrollView.bounces = TRUE;
[self createMenuWithButtonSize:CGSizeMake(70.0, 30.0) withOffset:20.0f noOfButtons:7];
Then in my function i have created my menu buttons which will be look like buttons on a navigation bar
-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons{
NSLog(#"inserting into the function for menu bar button creation");
for (int i = 0; i < totalNoOfButtons; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(mybuttons:) forControlEvents:UIControlEventTouchUpInside];
if(i==0){
[button setTitle:[NSString stringWithFormat:#"Dashboard"] forState:UIControlStateNormal];//with title
}
else if(i==1){
[button setTitle:[NSString stringWithFormat:#"Order"] forState:UIControlStateNormal];//with title
}
else if(i==2){
[button setTitle:[NSString stringWithFormat:#"Product"] forState:UIControlStateNormal];//with title
}
else if(i==3){
[button setTitle:[NSString stringWithFormat:#"Customers"] forState:UIControlStateNormal];//with title
}
else if(i==4){
[button setTitle:[NSString stringWithFormat:#"Content"] forState:UIControlStateNormal];//with title
}
else if(i==5){
[button setTitle:[NSString stringWithFormat:#"Site Analysis"] forState:UIControlStateNormal];//with title
}
else if(i==6){
[button setTitle:[NSString stringWithFormat:#"Store Settings"] forState:UIControlStateNormal];//with title
}
else if(i==7){
[button setTitle:[NSString stringWithFormat:#"CMS Settings"] forState:UIControlStateNormal];//with title
}
button.frame = CGRectMake(i*(offset+buttonSize.width), 8.0, buttonSize.width, buttonSize.height);
button.clipsToBounds = YES;
button.showsTouchWhenHighlighted=YES;
button.layer.cornerRadius = 0;//half of the width
//button.layer.borderColor=[UIColor clearColor];
button.layer.backgroundColor=[UIColor blueColor].CGColor;
button.titleLabel.font = [UIFont systemFontOfSize:10];
button.layer.borderWidth=0.0f;
button.tag=i;
[menuScrollView addSubview:button];
}
menuScrollView.contentSize=CGSizeMake((buttonSize.width + offset) * totalNoOfButtons, buttonSize.height);
[self.view addSubview:menuScrollView];
}
and it solves...Happy Coding... :))

Related

how to set the scroll view contentsize dynamically?

I had an application in which i am setting the content size of the scroll view dynamically according to the number of buttons which i am creating from an array.like this `
for(int i =0;i<[sarray count];i++)
{
NSMutableDictionary *dicttable=[sarray objectAtIndex:i];
NSString *head=[dicttable objectForKey:#"cat"];
btn= [UIButton buttonWithType:UIButtonTypeCustom];
int j=i+1;
btn.frame = CGRectMake((j-1)*87,0,87, 44);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:head forState:UIControlStateNormal];
btn.tag = i;
[btn setShowsTouchWhenHighlighted:YES];
[Scroller addSubview:btn];
[btn addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
if(btn.tag==0)
{
sendActionsForControlEvents:UIControlEventTouchUpInside];
}
else
{
btn.selected=NO;
}
}
}
[Scroller setContentSize:CGSizeMake([sarray count]*85, 44)];
`
but here the problem is after the last button also the scroll view scrolls .it need not to be happend,i need the scrolling ends with last button on the view,can anybody help me on this
set scrollview bounces to NO
scrollView.bounces = NO;
yes as you said it looses the smoothness. But no other choice. If you found any sol please update the answer.
hey i am sending you complete example to make button dynamicay and seting the content size scrollview on number of count ,i had just given u one example code u have to set the size of frame of button as u requried
UIScrollView *scroll_View=[[UIScrollView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:scroll_View];
int y=0;
for(int i =1;i<10;i++)
{
// NSMutableDictionary *dicttable=[sarray objectAtIndex:i];
// NSString *head=[dicttable objectForKey:#"cat"];
UIButton* btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(y,89,100, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
NSString *str=[NSString stringWithFormat:#"%d",i];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:str forState:UIControlStateNormal];
btn.tag = i;
[btn setShowsTouchWhenHighlighted:YES];
[scroll_View addSubview:btn];
[btn addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
y=110*i;
}
[scroll_View setContentSize:CGSizeMake(y, 400)];
enjoy coding
You just declare one yMargin variable of type CGFloat and initialize it with some required value as 0. Now every time whenever you are adding a new subview vertically do y+=lbl.frame.size.height;
Now setContentSize:CGSizeMake([sarray count]*85, y margin+someRequiredMargin)];
(someRequiredMargin - you want to add below )

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.

add button to uitextfield

When I click inside text box, How can I add plus button to UITextField as shown in the following screen? When this button is clicked it would launch iPhone contact application. I would very much appreciate any code example. Thanks
Following code use for adding button in UITextField.
-(void)ViewDidLoad{
UITextField *txt = [[UITextField alloc]initWithFrame:CGRectMake(140, 120, 160, 40)];
txt.returnKeyType = UIReturnKeyDone;
txt.placeholder = #"Enter or Mobile Number";
[txt setBorderStyle:UITextBorderStyleRoundedRect];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"crossImg.png"] forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
[button addTarget:self action:#selector(clearText:) forControlEvents:UIControlEventTouchUpInside];
txt.rightView = button;
txt.rightViewMode = UITextFieldViewModeAlways;
[self.view addSubview:txt];
}
-(IBAction)clearText:(id)sender{
}
You can use the below code for this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == yourFirstTextField)
{
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:#"addImage.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside];
textField.rightViewMode = UITextFieldViewModeWhileEditing;
textField.rightView = addButton;
}
}
You can use the above method if you added the field in IB.
If you are created it through code, you need to add the below code when creating:
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:#"addImage.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside];
textField.rightViewMode = UITextFieldViewModeWhileEditing;
textField.rightView = addButton;
Use following method for add UIButton
[self.txtField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
- (BOOL) textFieldDidChange:(UITextField *)textField
{
UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom];
[btnColor addTarget:self action:#selector(btnColorPressed:) forControlEvents:UIControlEventTouchUpInside];
btnColor.frame = CGRectMake(self.txtTag.bounds.size.width - 50, 5, 25, 25);
[btnColor setBackgroundImage:[UIImage imageNamed:#"PaintPickerButton.png"] forState:UIControlStateNormal];
[self.txtTag addSubview:btnColor];
return YES;
}
Or Write
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// self.scrollView.frame = CGRectMake(0, 35, self.view.bounds.size.width, self.view.bounds.size.height - 250);
[UIView commitAnimations];
UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom];
[btnColor addTarget:self action:#selector(btnColorPressed:) forControlEvents:UIControlEventTouchUpInside];
btnColor.frame = CGRectMake(150, 5, 25, 25);
[btnColor setBackgroundImage:[UIImage imageNamed:#"PaintPickerButton.png"] forState:UIControlStateNormal];
[self.txtField addSubview:btnColor];
return YES;
}
Use rightView property of UITextField.
In textFieldDidBeginEditing: delegate create button with action you want & set to rightView.
And in textFieldDidEndEditing: set nil to rightView
you can also go for custom view ant then add sub view to it. then you hide it. it will be visible only when the user taps it
self.locationName.clearButtonMode = UITextFieldViewModeWhileEditing;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 30, 30);
[button setImage:[UIImage imageNamed:#"goButtonIcon.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(goButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.locationName.rightView = button;
self.locationName.rightViewMode = UITextFieldViewModeUnlessEditing;
and then call this method:
-(IBAction)goButtonClicked:(id)sender{
}
self.locationName -------> UITextField Reference (in ViewDidload you can write this code)

iOS Programmatically Created Button not Working

Hi I have a UIButton that I created programmatically but unfortunately its not working. When I click on the UIButton nothing happens. I don't know what is the problem. Any help would be appreciated.
UIButton *connectedStories = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
connectedStories.frame = CGRectMake(10, 10, 1900, 30);
connectedStories.backgroundColor = [UIColor whiteColor];
[connectedStories setTitle:#"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
[label1 setText:storyDescription];
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];
[viewContainer addSubview:contentView];
return contentView;
- (void)buttonClicked
{
NSLog(#"button clicked");
}
I think labels recives touches instead of button.
Add userInteractionEnabled = NO; to your labels.
label.userInteractionEnabled = NO;
label1.userInteractionEnabled = NO;
Try changing the top part of your code to this:
CGRect frame = CGRectMake(10, 10, 1900, 30);
UIButton *connectedStories = [[UIButton alloc] initWithFrame:frame];
//set the position of the button
[connectedStories setBackgroundColor:[UIColor whiteColor]];
[connectedStories setTitle:#"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:nil action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
It may very well be the following:
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];
im sure you would need to add them in the following order:
[contentView addSubview:backGroundImageView];
[contentView addSubview:label];
[contentView addSubview:label1];
[contentView addSubview:connectedStories];
This is because when you call addSubview: it adds it to end of the receivers list of subViews and the most recent subView appears on top.
Check out the UIView Documentation here
the reason that the Interaction isn't being detected is that you are not passing the touchEvents through to the other subViews as they are being captured by label1 and stopping there.
By default the last subView captures all touch events and doesn't pass them on.
Add the colon in last of your method in selector
[connectedStories addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:#"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
[custombutton setImage:[UIImage imageNamed:#"hh.png"] forState:UIControlStateNormal];
[view addSubview:custombutton];

iPhone SDK: Non-transparent subviews in transparent view

I have a subview that has been added to my UIView. The idea is that the subview is a hovering panel with uibuttons. The subview is given an alpha of 0.2, as the user needs to be able to see what is behind it.
The problem is that when I add uibuttons to the subview, they inherit the alpha from the subview (I want the buttons to be non-transparent and have an alpha of 1.0). I've tried to tackle this problem by iterating through the uibuttons and setting their alpha back to 1.0 without success. Solutions?
// Create the subview and add it to the view
topHoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
topHoverView.backgroundColor = [UIColor blackColor];
[topHoverView setAlpha:0.2];
[self.view addSubview:topHoverView];
// Add button 1
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(20, 10, 80, 30)];
[button1 setTitle:#"New" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:button1];
// Add button 2
button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setFrame:CGRectMake(120, 10, 80, 30)];
[button2 setTitle:#"Clear" forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(button2Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:button2];
// Add button 3
button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button3 setFrame:CGRectMake(220, 10, 80, 30)];
[button3 setTitle:#"Delete" forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(button3Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:d button3];
// Attempt to iterate through the subviews (buttons) to set their transparency back to 1.0
for (UIView *subView in topHoverView.subviews) {
subView.alpha = 1.0;
}
You should create topHoverView with alpha 1.0 and a transparent background color. Then add a subview with a black background that covers the complete view with alpha 0.2 and then add the buttons to topHoverView:
// Create the subview and add it to the view
topHoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
topHoverView.backgroundColor = [UIColor transparentColor];
[topHoverView setAlpha:1.0];
[self.view addSubview:topHoverView];
canvasView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
canvasView.backgroundColor = [UIColor blackColor];
[canvasViewView setAlpha:0.2];
[topHoverView.view addSubview:canvasView];
// Add button 1
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(20, 10, 80, 30)];
[button1 setTitle:#"New" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:button1];
// Add button 2
button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setFrame:CGRectMake(120, 10, 80, 30)];
[button2 setTitle:#"Clear" forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(button2Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:button2];
// Add button 3
button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button3 setFrame:CGRectMake(220, 10, 80, 30)];
[button3 setTitle:#"Delete" forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(button3Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:d button3];
// Attempt to iterate through the subviews (buttons) to set their transparency back to 1.0
for (UIView *subView in topHoverView.subviews) {
subView.alpha = 1.0;
}
Instead of
topHoverView.backgroundColor = [UIColor transparentColor];
, the following code is right.
topHoverView.backgroundColor = [UIColor clearColor];