how to make textView expandable by clicking on a button - iphone

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.

Related

How to make expandable/collapsable ImageView

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/

Something strange with my NavBar

i have an iPhone app with TabBarController that each tab has a Nav Controller.. one of the Nav Controller pushes to another Viewcontroller. in that View Controller i am trying to Hide the back button and place One of my custom buttons. in the
- (void)viewDidLoad
{
[super viewDidLoad];
self._allUsersParticipantsInTheVideo = [[NSMutableArray alloc] init];
self._allUsersInvitesInTheVideo = [[NSMutableArray alloc] init];
for (int i=0; i < [self._curVideoToShow._videoParticipants count]; i++) {
[self._allUsersParticipantsInTheVideo addObject:[self._curVideoToShow._videoParticipants objectAtIndex:i]];
}
for (int i=0; i < [self._curVideoToShow._videoInvitees count]; i++) {
[self._allUsersInvitesInTheVideo addObject:[self._curVideoToShow._videoInvitees objectAtIndex:i]];
}
[self setNavBar];
}
#pragma mark - Set NavBar
-(void)setNavBar{
self.navigationItem.hidesBackButton = YES;
UIView *navBarItemview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navBarItemview setBackgroundColor:[UIColor clearColor]];
// Set cutsom back button
//UIImage *backImage = [UIImage imageNamed:#"backButton.png"];
UIImage *backImage = [UIImage imageNamed:#"LeftArrowBg.png"];
CGRect frame = CGRectMake(0, 6, backImage.size.width + 4, backImage.size.height);
UIButton *bttn = [[UIButton alloc] initWithFrame:frame];
[bttn setBackgroundImage:backImage forState:0];
[bttn setTag:kBackBttn];
[bttn setTitle:#"Back" forState:UIControlStateNormal];
[bttn setTitleEdgeInsets:UIEdgeInsetsMake(4, 5, 0, 0)];
[bttn.titleLabel setFont:[UIFont fontWithName:#"PTSans-Bold" size:12.0]];
[bttn setContentMode:UIViewContentModeScaleAspectFit];
[bttn addTarget:self action:#selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[navBarItemview addSubview:bttn];
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 4, 320, 40)];
[titleLbl setFont:[UIFont fontWithName:#"PTSans-Bold" size:24.0]];
[titleLbl setTextColor:[UIColor whiteColor]];
[titleLbl setTextAlignment:UITextAlignmentCenter];
[titleLbl setBackgroundColor:[UIColor clearColor]];
[titleLbl setTag:kTitleLbl];
[titleLbl setShadowOffset:CGSizeMake(0, 1)];
[titleLbl setShadowColor:[UIColor blackColor]];
[titleLbl setText:#"All Participants"];
[navBarItemview addSubview:titleLbl];
self.navigationItem.titleView = navBarItemview;
}
As you can see i've written :
self.navigationItem.hidesBackButton = YES;
but on the first time when the app is lunched, i see the back button of the NavBar only for the first time, but later on for every time that i open the app the button remains to be my custom one. ( and also when i Navigate it is my custom )
someone?
try with this..
-(void)viewWillAppear:(BOOL)animated
{
self.navigationItem.hidesBackButton = YES;
}
or
[self.navigationController.navigationItem setHidesBackButton:YES];
or
self.navigationController.navigationItem.backBarButtonItem = nil;

Show UIButton Done on UIScrollView for Images only when user taps on image view

How can i code for UIButton to show only when user taps on UIScrollView for images.
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 10, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
Right now it is displaying on every image view but i want it should Show UIButton Done only when taps on the screen.
EDIT: If i add
[imageView addGestureRecognizer: tap];
and initiate Gesturerecognizer
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
[tap release];
then how can i code in handle tap method to show done button when user taps on image view
- (void)handleTap:(UIGestureRecognizer*)tap {
}
Thanks for help.
You need to add the gesture recognizer on the image view and not on the ViewController.
Also, you need to enable the user interaction for the image view like this:
imageView.userInteractionEnabled = YES;
Check UIScrollViewDelegate Method when you will scroll show your button will be shown and then if you want to hide that button hide it.
And for handleTap you set [imageView addGestureRecognizer:tap]; instead of [self addGestureRecognizer:tap];
create your button in ViewDidLoad and hide it initially and when user will tap image just set`
button.hidden = NO;

How to differentiate selected, non selected section on segmented controller

I have used the following code to create a segmented controller, but I can't differentiate which is selected and which is not selected. How do I differentiate?
UISegmentedControl *segmentedControl;
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
[segmentedControl insertSegmentWithTitle:#"Male" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:#"Female" atIndex:1 animated:YES];
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.frame = CGRectMake(100,10,200,30);
[segmentedControl setMomentary:YES];
[segmentedControl addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
- (void)segmentSwitch:(id)sender
{
segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
UIView *firstView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20,20)];
firstView.backgroundColor=[UIColor greenColor];
UIView *secondView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20,20)];
firstView.backgroundColor=[UIColor brownColor];
if (selectedSegment == 0)
{
NSLog(#"first segment");
//toggle the correct view to be visible
strGender =[[NSMutableString alloc]initWithString:#"Male"];
[firstView setHidden:NO];
[secondView setHidden:YES];
}
else
{
NSLog(#"second segment");
//toggle the correct view to be visible
strGender =[[NSMutableString alloc]initWithString:#"Female"];
[firstView setHidden:YES];
[secondView setHidden:NO];
}
}
There are a few things here. Based on your comment to #Surjit's answer, you will have to use insertSegmentWithImage:atIndex:animated if you want to change the color of the segment. You will need to have images for each segment for both selected and non-selected state.
But there are few problems in your segmentSwitch: method. You are creating both firstView and secondView but not adding them to the view hierarchy. You are setting the background color of firstView twice. You probably intended one of the calls to be to secondView. And there's no point changing the hidden property of the two views without them being on the screen. If you are looking to switch between two views of different colors, then declare them as ivars and initialize them elsewhere and then switch their hidden on segment switch.
UISegmentedControl *segmentedControl;
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
segmentedControl.frame = CGRectMake(0, 6, 320, 40);
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.tintColor = [UIColor blackColor];
segmentedControl.backgroundColor = [UIColor blackColor];
[segmentedControl setMomentary:YES];
[segmentedControl addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
// here is the code how to differenciate selected , non selected section on segmented //controller
int selectedSegment = segmentedControl.selectedSegmentIndex;
if(selectedSegment == 0)
{
// code 1
}
else if(selectedSegment == 1)
{
// code 2
}enter code here
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UISegmentedControl *segmentedControl; // add this to your (.h) file
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
[segmentedControl insertSegmentWithTitle:#"Red" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:#"Green" atIndex:1 animated:YES];
[segmentedControl insertSegmentWithTitle:#"Blue" atIndex:2 animated:YES];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
segmentedControl.frame = CGRectMake(0, 0, 320, 40);
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.tintColor = [UIColor blackColor];
segmentedControl.backgroundColor = [UIColor blackColor];
[segmentedControl setMomentary:NO]; // imp property (change it & see magic)
[segmentedControl addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
}
(IBAction)segmentSwitch:(id)sender {
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
if (selectedSegment == 0)
self.view.backgroundColor = [UIColor blueColor];
else if (selectedSegment == 1)
self.view.backgroundColor = [UIColor greenColor];
else if (selectedSegment == 2)
self.view.backgroundColor = [UIColor redColor];
}
Just copy & paste this code.
see this example here i am setting tint color for selected
and unselected segment index.
But before that please unchecked the momentary state from xib
for UISegmentedControl.
- (void)segmentAction:(id)sender{
UIColor *tintcolor1=[UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1.0];
UIColor *tintcolor2=[UIColor colorWithRed:211/255.0 green:78/255.0 blue:65/255.0 alpha:1.0];
for (int i=0; i<[segment_controller.subviews count]; i++)
{
if ([[segment_controller.subviews objectAtIndex:i]isSelected] )
{
[[segment_controller.subviews objectAtIndex:i] setTintColor:tintcolor2];
}else
{
[[segment_controller.subviews objectAtIndex:i] setTintColor:tintcolor1];
}
}

Changing the content of the UIScrollView iPhone

I am trying to create a scrollable menu bar (which I am succeeding in doing) but now instead I want to add a large button with an image centred in the button, instead of taking up the total width and height of the button
THis is my current code:
[scrollView setBackgroundColor:[UIColor whiteColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
UIImage *image;
UIImageView *iv;
UIButton *button;
count = [returned count];
for (int i=0; i<count; i++)
{
image = [UIImage imageNamed:#"coat2.jpg"];
iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,105,120)];
button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,105,120)];
[button setAlpha:100];
[button setTag:1];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
iv.image = image;
iv.tag=#"coat2.jpg";
iv.userInteractionEnabled = YES;
[iv insertSubview:button atIndex:0];
[button release];
[scrollView addSubview:iv];
[iv release];
}
[scrollView setScrollEnabled:YES];
[self layoutScrollImages];
scrollView.delegate = self;
the above code creates an image and button over it where the button and image are of the exact same width and height. I want the button to be significantly larger both in height and width.
Is this possible?
Thanks!
2nd attempt (using help from Joseph Tura)
[scrollView setBackgroundColor:[UIColor whiteColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
UIImage *image;
count = [returned count];
for (int i=0; i<count; i++)
{
image = [UIImage imageNamed:#"coat2.jpg"];
iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,20,105,120)];
button = [[ProductButton alloc] initWithFrame:CGRectMake(0,20,105,120)];
[button setAlpha:100];
[button setTag:1];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
iv.tag=tag;
iv.userInteractionEnabled = YES;
iv.image = myimage;
UIView *uiView = [[UIView alloc] initWithFrame:CGRectMake(0,20,105,120)];
[uiView addSubview:iv];
button = [[ProductButton alloc] initWithFrame:CGRectMake(0,0,210,240)];
button.center = CGPointMake(uiView.frame.size.width /2, uiView.frame.size.height/2);
[button setTag:1];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[uiView addSubview:button];
[scrollView addSubview:uiView];
}
[scrollView setScrollEnabled:YES];
[self layoutScrollImages];
scrollView.delegate = self;
-----
-(void)layoutScrollImages
{
//UIImageView *view = nil;
UIView *view = nil;
NSArray *subviews = [scrollView subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += 110;
}
}
[scrollView setContentSize: CGSizeMake(count*110, [scrollView bounds].size.height)];
}
Why not put both elements inside a UIView?
UIView *aView = [[UIView alloc] initWithFrame:iv.frame];
[aView addSubview:iv];
button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,210,240)];
button.center = CGPointMake(aView.frame.size.width / 2, aView.frame.size.height / 2);
[aView addSubview:button];