Add a custom button to ABUnknownPersonViewController - iphone

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.

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/

Want to add the UIActivityIndicator beside the UIBarButtonItem

How can i add the activity indicator beside the right bar button item (CreateNew button)?
One of the approaches that you can use is initializing the Bar Button using some custom view.
A bar minimum code which can help you get some hint is
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIView* aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 40)];
[aView setBackgroundColor:[UIColor blackColor]];
[[aView layer] setCornerRadius:8.0f];
[[aView layer] setBorderColor:[UIColor whiteColor].CGColor];
UIActivityIndicatorView* loadView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[loadView startAnimating];
[aView addSubview:loadView];
[loadView setFrame:CGRectMake(5, 5, 30, 30)];
[loadView release];
UIButton* aButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 2, 80, 35)];
[aButton setImage:[UIImage imageNamed:#"btnLogin.png"] forState:UIControlStateNormal];
[aView addSubview:aButton];
[aButton release];
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:aView];
[self.navigationItem setRightBarButtonItem:barButton];
[aView release];
[barButton release];
}
This is If you do it programmatically , else you can also make use of nib. Creating bar button this way will look something like this -
You can look for more option making using of [[UIBarButtonItem alloc] initWithCustomView:aView]; method.
Hope it helps!!
For this try to customize UINavigationController Class.
Add Activity Indicator on navigationBar and control it from your ViewController.
You can add the activity indicator using the following code:
//Create an instance of activity indicator view
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
//set the initial property
[activityIndicator stopAnimating];
[activityIndicator hidesWhenStopped];
//Create an instance of Bar button item with custome view which is of activity indicator
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
//Set the bar button the navigation bar
[self navigationItem].rightBarButtonItem = barButton;
//Memory clean up
[activityIndicator release];
[barButton release];
Please refer this link for detailed code.

UISearchBar with UINavigationController

I added a UISearchBar and UISearchDisplayController with a UINavigationController. Because we needed a few buttons in the navigationBar, I created a custom view that contains a few buttons, and put that as the rightBarButtonItem. I notice when I click on the search bar, the keyboard pops up from the bottom, and also moves the UINavigationController's navigationBar off the screen. So now I cannot see what I enter in my UISearchBar. Is there a way to get around this? I've seen other apps that it looks like they use a UINavigationBar + multiple buttons on the LHS or RHS of the navigationBar, so I'm assuming this is possible. Thanks.
UIButton *homeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
UIImage *house = [UIImage imageNamed:#"icon_house.png"];
[homeButton setImage:house forState:UIControlStateNormal];
[homeButton addTarget:self action:#selector(homePressed:) forControlEvents:UIControlEventTouchUpInside];
UIButton *filterButton = [[UIButton alloc] initWithFrame:CGRectMake(49, 0, 44, 44)];
[filterButton setTitle:#"Filter" forState:UIControlStateNormal];
[filterButton addTarget:self action:#selector(FilterButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.SearchEntry = [[[UISearchBar alloc] initWithFrame:CGRectMake(98, 0, 150, 44)] autorelease];
self.SearchEntry.delegate = self;
UISearchDisplayController *searchDisplayCtlr = [[UISearchDisplayController alloc] initWithSearchBar:_searchEntry contentsController:self];
searchDisplayCtlr.searchResultsDataSource = self;
searchDisplayCtlr.searchResultsDelegate = self;
searchDisplayCtlr.delegate = self;
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 248, 44)];
[containerView addSubview:homeButton];
[containerView addSubview:filterButton];
[containerView addSubview:_searchEntry];
Try to adjust the contentSize of the UIScrollView. Also please take care of the Hierarchy of the controls you have taken. Dont keep UINavigationBar and UISearchView in ScrollView, then it will not move and remains fixed in their position

UIScrollView with UITapGestureRecognizer and UIPageControl

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

customView on left button of UINavigationBar

I'm trying to implement UINavigationBar with custom controls. So, I've added UIView on left side of UINavigationBar and trying to add controls to that UIView with following code:
UIView *view = navigationController.navigationItem.leftBarButtonItem.customView;
UIButton *button = [[UIButton alloc] initWithFrame:view.frame];
[button setTitle:#"TEST" forState:UIControlStateNormal];
[view addSubview:button];
[button sizeToFit];
[button release];
Code works, but button doesn't appear.
Any help would be appreciated.
Thank you.
UPD
OK, I gave a try to code below and made such thing:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 50)];
[button setTitle:#"XXX" forState:UIControlStateNormal];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:button];
navigationController.visibleViewController.navigationItem.leftBarButtonItem = customItem;
[customItem release];
[button release];
The "XXX" title did appear, but it looks like simple label, not button. Any ideas?
Yes, your UIButton doesn't come with any shading. What I've done is used a UISegmentedControl to get shading for free:
// Add the Menu button to the navigation bar
NSString* menuLabel = "ShadeButton";
CGSize textSize = [menuLabel sizeWithFont:[UIFont systemFontOfSize:15.0]];
UISegmentedControl* menuSegmentedButton = [[UISegmentedControl alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, textSize.width, 29.0f)];
menuSegmentedButton.momentary = YES;
menuSegmentedButton.selected = NO;
menuSegmentedButton.segmentedControlStyle = UISegmentedControlStyleBar;
[menuSegmentedButton insertSegmentWithTitle:menuLabel atIndex:0
animated:NO];
[menuSegmentedButton addTarget:self action:#selector(doMenu)
forControlEvents:UIControlEventValueChanged];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc]
initWithCustomView:menuSegmentedButton];
[menuSegmentedButton release];
self.navigationItem.rightBarButtonItem = barButton;
Create your UIBarButtonItem with the UISegmentedControl as shown above rather than a UIButton and you should get the effect you're after. The alternative is to do more work on the button and create a texture/custom image for it yourself.
Note quite right.
If you just want a different title:
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"TEST" style:UIBarButtonItemStylePlain target:nil action:nil];
navigationController.navigationItem.leftBarButtonItem = customItem;
[customItem release];
If you really want a custom view:
// Create Custom View called myView.
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:myView];
navigationController.navigationItem.leftBarButtonItem = customItem;
[customItem release];