How to display two buttons in popoverview - WEPOPOVER - iphone

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)];

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 )

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];

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];

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.

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];