I have many textViews on top of a scrollView and every textView has a custom button over it so that when user click that textView it should expand and when it click back then it should collapse to previous position.
what I'm thinking of doing is hide the smallTextView and show the expandedTextView when button is pressed and when the button is pressed i want to hide expandedtextView n show the smallTextView. but I don't know how I should do it. any help will be appreciated.
Here is my code:
-
(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = #"Demo";
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
for (int i=0;i<[appDelegate.serverResponseArray count];i++)
{
self.expandTextView = [[UITextView alloc] init];
[self.expandTextView setFrame:CGRectMake(8.0f, i*50.0f+10.0f, 270.0f, 40.0f)];
[self.expandTextView setBackgroundColor:[UIColor grayColor]];
[self.expandTextView setFont:[UIFont fontWithName:#"helvetica" size:12]];
[self.expandTextView setText:#"Welcome!!!"];
[self.expandTextView setTextColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1]];
[self.expandTextView setUserInteractionEnabled:NO];
[self.scrollView addSubview:self.expandTextView];
self.expandTextView = nil;
self.expandButton = [[UIButton alloc]initWithFrame:CGRectMake(8.0f, i*50.0f+1.0f, 270.0f, 60.0f)];
[self.expandButton setBackgroundColor:[UIColor clearColor]];
[self.expandButton addTarget:self action:#selector(textButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.expandButton.tag = i;
[self.scrollView addSubview:self.expandButton];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(278.0f, i*50.0f+10.0f, 14.0f, 40.0f)];
[imageView setBackgroundColor:[UIColor grayColor]];
[imageView setImage:[UIImage imageNamed:#"arrow.png"]];
[self.scrollView addSubview:imageView];
imageView = nil;
}
float maxHeight = 0;
for(UIView *v in [self.scrollView subviews])
{
if(v.frame.origin.x + v.frame.size.height > maxHeight)
maxHeight = v.frame.origin.x + v.frame.size.height;
}
self.scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, maxHeight+2570);
}
-(IBAction)textButtonClicked:(id)sender
{
NSLog(#"%#",sender);
}
and how do I know which button is getting pressed.
To know which button press you have to assign tag value to your button then
-(IBAction)textButtonClicked:(id)sender
{
UIButton *btn=(UIButton*)sender;
NSLog(#"Tag of button = %d",btn.tag);
}
And i suggest you to change frame of your existing imageView instead of using other image view(hide and show)
First of all , I'd suggest you to use UITableView instead of UIImageView and then implement Expandable and Collapsible UITableViewCell.
There are lots of Tutorial to implement this Functionality. Simply Google it and you will have plenty of solutions for that.
For Example : Expandable/Collapsible Table For iOS
Check this Sample Code provided by Apple : Table View Animations and Gestures.
The Best way is Take UITableView and Put your images on Cell of UITableView and change size of cell when your click on it, i give you simple example for how to create expandable UITableViewCell.
http://www.roostersoftstudios.com/2011/04/14/iphone-uitableview-with-animated-expanding-cells/
Related
I have many textViews on top of a scrollView and every textView has a custom button over it so that when user click that textView it should expand and when it click back then it should collapse to previous position.
what I'm thinking of doing is hide the smallTextView and show the expandedTextView when button is pressed and when the button is pressed i want to hide expandedtextView n show the smallTextView. but I don't know how i should do it. any help will be appreciated.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = #"Demo";
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
for (int i=0;i<[appDelegate.serverResponseArray count];i++)
{
self.expandTextView = [[UITextView alloc] init];
[self.expandTextView setFrame:CGRectMake(8.0f, i*50.0f+10.0f, 270.0f, 40.0f)];
[self.expandTextView setBackgroundColor:[UIColor grayColor]];
[self.expandTextView setFont:[UIFont fontWithName:#"helvetica" size:12]];
[self.expandTextView setText:#"Welcome!!!"];
[self.expandTextView setTextColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1]];
[self.expandTextView setUserInteractionEnabled:NO];
[self.scrollView addSubview:self.expandTextView];
self.expandTextView = nil;
self.expandButton = [[UIButton alloc]initWithFrame:CGRectMake(8.0f, i*50.0f+1.0f, 270.0f, 60.0f)];
[self.expandButton setBackgroundColor:[UIColor clearColor]];
[self.expandButton addTarget:self action:#selector(textButtonClicked:) forControlEvents:UIControlEventTouchDragInside];
[self.scrollView addSubview:self.expandButton];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(278.0f, i*50.0f+10.0f, 14.0f, 40.0f)];
[imageView setBackgroundColor:[UIColor grayColor]];
[imageView setImage:[UIImage imageNamed:#"arrow.png"]];
[self.scrollView addSubview:imageView];
imageView = nil;
}
float maxHeight = 0;
for(UIView *v in [self.scrollView subviews])
{
if(v.frame.origin.x + v.frame.size.height > maxHeight)
maxHeight = v.frame.origin.x + v.frame.size.height;
}
self.scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, maxHeight+2570);
}
-(void)textButtonClicked:(id)sender
{
NSLog(#"Hi");
[self.expandTextView setHidden:YES\];
NSLog(#"hey");
}
and how do I know which button is getting pressed.
You are doing it wrong. Your method should look like this :
-(IBAction)textButtonClicked:(id)sender
{
NSLog(#"Hi");
[self.expandTextView setHidden:YES];
NSLog(#"hey");
}
After writing this Method in your ViewContrller, wire up your Expand Button's -touchUpInside Method to -textButtonClicked Method. Use this line :
[self.expandButton addTarget:self action:#selector(textButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
instead the one in your code.
I'm trying to create a UITableView with multiple sections. Each section has it's own header, but I am trying to make a universal footer for the entire table view that stays in one position...
Is this logic possible using the UITableViewDelegate methods? Or should I create a custom view and just try and add it as a subview to my table view? The footer I currently have contains a UIButton.
If anyone has some sample code that would be great.
Edit: This question is not the same as the one referenced. I am trying to make a universal footer that floats above the UITableView. The other question does not specify the location of the footer, only that a footer is desired.
read about UITableView's tableFooterView property.
And now some code: (using ARC)
UILabel *footer = [UILabel alloc] init];
footer.text = #"Some text" ;
footer.backgroundColor = [UIColor greenColor];
self.tableView.tableFooterView = footer;
Now at the bottom of entire tableview there is a UILabel that is green with some text.
I ended up creating a subview and adding my button to that view. I then made that view my "footer".
Here is the code that gave me the desired results.
- (void)viewDidLoad
{
[super viewDidLoad];
//self.tableView.delegate = self;
//self.tableView.dataSource = self;
//save current tableview, then replace view with a regular uiview
self.tableView = (UITableView*)self.view;
UIView *replacementView = [[UIView alloc] initWithFrame:self.tableView.frame];
self.view = replacementView;
[self.view addSubview:self.tableView];
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 370, 320, 45)];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.userInteractionEnabled = YES;
//the button should be as big as a table view cell
//width of the button can be set but the width of the view it is added to will always match the width of the tableView
[button setFrame:CGRectMake(60, 0, 200, 45)];
//set title, font size and font color
[button setTitle:#"Build" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//set action of the button
[button addTarget:self action:#selector(buildThenSegue)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[footerView addSubview:button];
footerView.userInteractionEnabled = YES;
[self.view addSubview:footerView];
self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
Take note that this is in a subclass of UITableViewController.
I referenced this answer to another question: https://stackoverflow.com/a/9084267/1091868
I have a uitableview with 2 cells (custom tableviewcells) and a button for login. I made them programmatically. Now I want to put a background image behind the 2 custom cells and the button. I went into IB and put the image view on top of the tableview and then put a picture on it which I want as the background. Then I went into viewDidLoad and put this bit of code tblSimpleTable.backgroundColor = [UIColor clearColor]; to clear the tableviews background colour.
It now looks like this:
But the background does not appear. Does anyone know how to fix this?
Thanks.
viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
self.dataSourceArray = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
#"UITextField", kSectionTitleKey,
#"TextFieldController.m: textFieldNormal", kSourceKey,
self.textFieldNormal, kViewKey,
nil],
nil];
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
loginButton.frame = CGRectMake(60,140,200,38); // position in the cell and set the size of the button
[loginButton setTitle:#"Login" forState:UIControlStateNormal];
// add targets and actions
[loginButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[self.view addSubview:loginButton];
tblSimpleTable.backgroundColor = [UIColor clearColor];
//tblSimpleTable.backgroundView = nil;
//UIImageView *imageView = [[UIImageView alloc] initWithImage:#"Background-Red.png"];
//imageView.frame = tblSimpleTable.frame;
//tblSimpleTable.backgroundView = imageView;
self.title = NSLocalizedString(#"TextFieldTitle", #"");
// we aren't editing any fields yet, it will be in edit when the user touches an edit field
self.editing = NO;
}
Why don't you use backgroundView property of the UITableView object?
UIImageView * imageView = [[UIImageView alloc] initWithImage:yourBackgroundImage];
imageView.frame = tblSimpleTable.frame;
tblSimpleTable.backgroundView = imageView;
Of course, backgroundView will be resized if you don't set the frame so skipping the second line should also not be a problem.
What you have should probably work out. Still try this :
Add
tblSimpleTable.backgroundView = nil;
in viewDidLoad
I'm creating a horizontal scrolling tableview containing images. I have the swiping functionality working great, and am able to add the images to the scrollView as so in cellForRowAtIndexPath:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 78)];
[scrollView setContentSize:CGSizeMake(700, 78)];
UIImage *footImage = [UIImage imageNamed:#"Foot.png"];
UIImageView *footImageView = [[UIImageView alloc] initWithImage:footImage];
footImageView.frame = CGRectMake(0, 0, 80, 78);
footImageView.userInteractionEnabled = YES;
[scrollView addSubview: footImageView];
UIImage *handImage = [UIImage imageNamed:#"Hand.png"];
UIImageView *handImageView = [[UIImageView alloc] initWithImage:handImage];
handImageView.frame = CGRectMake(90, 0, 80, 78);
handImageView.userInteractionEnabled = YES;
[scrollView addSubview: handImageView];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(highlightImage)];
[scrollView addGestureRecognizer:tapGesture];
NSLog(#"tapGesture added to scrollView");
[[cell contentView] addSubview:scrollView];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 50, tv.frame.size.width, 50)];
[pageControl setNumberOfPages:4];
[[cell contentView] addSubview:pageControl];
My tapGesture is registering with the selector, hightlightImage, and just logging that its calling this method correctly.
- (void) highlightImage
{
NSLog(#"tapping tapping");
}
What I REALLY am working for is the ability to highlight/select these images if tapped, and highlight/deselect them if tapped again. I will have another button on screen that will navigate to the next page (irrelevant here).
Am I going down the right path here? Obviously I will populate an NSArray of UIImages and populate the scrollView that way, but just spiking it out for now. If someone could give me some direction or an example of how to make each button separately selectable/de-selectable, that would be GREAT:)
Use UIButton instead of UIImageView.
It's got built-in selected functionality.
get rid of your gestures and add highlightImage as the action to every button.
make your highlightImage into highlightImage:(id)sender
sender should be a button so you can just do
[sender setSelected:YES];
I am using a ABUnknownPersonViewController for displaying contact information within UINavigatonController and I want a button at the bottom which will push another viewController with additional information on that user. I have been able to add a button to the bottom and I can scroll down and view it, but it does not stay on screen.
I am looking for the same thing, but can you please show me how did you add a button at the bottom?
What I did is add a button at the top, inside the navigation bar, although it does have a few visual problems, this is how i did it.
//I created a UIToolBar
UIToolBar *myToolBar = [[UIToolBar alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
//then set the style to match my navBar
[myToolBar setBarStyle:UIBarStyleBlackOpaque];
//and finally created the button.
NSMutableArray *buttons = [NSMutableArray alloc] initWithCapacity:1]autorelease];
UIBarButtonItem *details = [[[UIBarButtonItem alloc] init]autorelease];
[details setTitle: #"details"];
//after that placed it on top of the UPVC
unknownPersonViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myToolBar];
I hope this helps you in some way...
- (void)loadView
{
[super loadView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Horray Button!!" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 500.0, 160.0, 40.0);
//[self.view addSubview:button];
NSArray *subviews = [self.view subviews];
NSLog(#"subviews: %d", [subviews count]);
UIScrollView *v = [subviews objectAtIndex:0];
[v addSubview:button];
CGSize cgSize = [v contentSize];
cgSize.height = 500.0 + 40.0;
[v setContentSize:cgSize];
}
The code I am using to add the button down at the bottom, but it does not stay in view once scrolled to.