iPhone - custom chevron icon - iphone

Is it possible to swap out the icon for UITableViewCellAccessoryDetailDisclosureButton with a custom icon?

Sure. Set the accessoryView to a custom image view.

Use this in viewForAnnotation:
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeCustom];
advertButton.frame = CGRectMake(0, 0, 23, 23);
advertButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
advertButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[advertButton setImage:[UIImage imageNamed:#"button-image.png"] forState:UIControlStateNormal];
[advertButton addTarget:self action:#selector(advertFunction:) forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = advertButton;
Dont forget to include the image in your app and the function the selector is assigned too.

Related

How to add a button in UIScrollView with addSubview?

I am new to iOS development and I have a problem with UIScrollView. First of all, I've created a Scroll View in a storyboard and added
#property (retain, nonatomic) IBOutlet UIScrollView *mainScroll;
in view controller handler
In the viewDidLoad method I have the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mainScroll.contentSize = CGSizeMake(100.0,100.0); // Have to set a size here related to your content.
// You should set these.
self.mainScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mainScroll.minimumZoomScale = .5; // You will have to set some values for these
self.mainScroll.maximumZoomScale = 2.0;
self.mainScroll.zoomScale = 1;
UIButton* b = [[UIButton alloc] init];
[b setBounds:CGRectMake(.0f,0.f,100.0,100.0)];
[self.mainScroll addSubview:b];
}
But the button does not appear in the simulator. However if I add a button on storyboard in the IB, the button appears and I can scroll the view.
How can I add the button in code?
Try this code
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.mainScroll addSubview:button];
bounds and frame are not the same thing.
Use this code instead...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 44);
[self.mainScroll addSubview:button];
You need to set scroll view contents size, something like the following code.
[self.mainScroll setContentSize:CGSizeMake(width, height)];
Try this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(self.mainScroll.frame.origin.x + 25 ,self.mainScroll.frame.origin.y +25, 50, 44);
[self.mainScroll addSubview:button];
Best and easiest way to make a button from code is:
UIButton* yourButton = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
[yourButton setTitle:#"Your Title" forState:UIControlStateNormal];
// if you set the button's image the title won't be visible, because button's title is "under" the image. I recomend you to use this if you have a cut and dried image.
[yourButton setImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
// if you set the button's background image, button's title will be visible
[yourButton setBackgroundImage:[UIImage imageNamed:#"yourBackgroundImage.png"] forState:UIControlStateNormal];
// you can add target to the button, for handle the event when you touch it.
[yourButton addTarget:self action:#selector(handleToucUpInside:) forControlEvents:UIControlEventTouchUpInside];
// then add the button
[self.view addSubview:yourButton];
- (void) handleToucUpInside:(id)sender{
// here you can do anything you want, to handle the action
}

How to create multiline backBarButtonItem in UINavigationBar

I want to create multiline BackBarButtonItem. right now my back button display like this,
but i want to display it like this, color does not matter,
How can I do that? And this is my default back button which added while i am pushing my childviewcontroller thru my parentviewcontroller so i dont want to remove it.
You can do it using custom image,UILabel(MSLabel),UIButton.
MSLabel is used to change space between two line of UILabel. Because UILabel Does not provide any property to do that.
After you add MSLabel in your project use this code. Recommend size for backbtn.png is 48*30.
#import "MSLabel.h"
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the custom back button
UIImage *buttonImage = [[UIImage imageNamed:#"backbtn.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
MSLabel *lbl = [[MSLabel alloc] initWithFrame:CGRectMake(12, 4, 65, 28)];
lbl.text = #"Account Summary";
lbl.backgroundColor = [UIColor clearColor];
//lbl.font = [UIFont fontWithName:#"Helvetica" size:12.0];
lbl.numberOfLines = 0;
lbl.lineHeight = 12;
lbl.verticalAlignment = MSLabelVerticalAlignmentMiddle;
lbl.font = [UIFont fontWithName:#"Helvetica" size:12];
[button addSubview:lbl];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 70, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
}
-(void)back {
NSLog(#"Dilip Back To ParentViewController");
// Tell the controller to go back
[self.navigationController popViewControllerAnimated:YES];
}
Outcome will display like this, Use your image for better result.
In this case you should use custom button and use background image on this button.Because i think it may not be possible.See for custom button
UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(10.0f,6.5f,60.0f,30.0f)];
[backBtn setBackgroundImage:[UIImage imageNamed:#"accountSummary"] forState:UIControlStateNormal];
[backBtn setBackgroundImage:[UIImage imageNamed:#"accountSummary"] forState:UIControlStateHighlighted];
[backBtn addTarget:self action:#selector(accountSummaryBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
[self.navigationItem setLeftBarButtonItem:leftButton];
- (void)accountSummaryBtnPressed:(id)sender
{
//--------- do stuff for account summary
}
What i believe you can Create CustomView with ImageView And Transparent label with numberOflines more than two or whatever you want to restrict it to , then replace the navigation back button with That Barbutton initiated with CustomVIew, That Can Solve your problem,, Hope fully

Change UIButton highlighted image in UINavigationBar

I'm trying to change a UIImage in a UIButton, when highlighted. The UIButton is situated in a UINavigationController.
I have the following code:
UIView *containingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 28, 28)];
UIButton *barUIButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barUIButton setImage:[UIImage imageNamed:#"Add.png"] forState:UIControlStateNormal];
barUIButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
barUIButton.frame = CGRectMake(-9, 0, 28, 28);
[barUIButton addTarget:self action:#selector(addButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[barUIButton setImage:[UIImage imageNamed:#"AddHighlighted.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
[containingView addSubview:barUIButton];
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];
self.navigationItem.rightBarButtonItem = containingBarButton;
Instead of the new image showing when highlighted, there is just a black shadow around the existing image.
Why is this?
It looks like the UIButton instance isn't getting highlighted or selected on the touch down event. This is probably because UIBarButtonItem instances don't act like normal buttons; in fact, they're not even UIButton subclasses.
There is a workaround. If you keep a reference to your UIButton in an instance variable, you can add code to change the button's image:
[barUIButton addTarget:self action:#selector(pressDown:) forControlEvents:UIControlEventTouchDown];
[barUIButton addTarget:self action:#selector(pressUp:) forControlEvents:UIControlEventTouchUp];
In pressDown: and pressUp:, you can set the
-(void)pressDown:(id)sender
{
[barUIButton setImage:[UIImage imageNamed:#"Add.png"] forState:UIControlStateNormal];
}
And similarly for pressUp:.

iphone customizing UIButtons

how to customize the UIButton so that it will display other than the default button style.
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(10.0, 20.0, 50.0, 50.0); // You can change the x, y, width, height.
[btn setImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
[self.view addSubView:btn];
You can do something like this:
UIImage *btnImage = [UIImage imageNamed:#"images-01.png"];
UIButton *btnGo = [UIButton buttonWithType:UIButtonTypeCustom];
btnGo.frame = CGRectMake(85,123, 100, 26);
btnGo.backgroundcolor = [UIColor clearColor];
btnGo. setImage:btnImage forState: UIcontrolStateNormal];
[btnGo.titleLabel setFont: [UIFont systemFontOfSize:14]];
btnGo addTarget: self action:#selector(btnGoClicked) forControlEvents: UIControlEventTouchUpInside];
You can use this
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame = CGRectMake(899, 5, 82, 55);
customButton.tag = 123;
[customButton addTarget:self action:#selector(buttonToggleForHide) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:customButton];
and make event for that using
-(void)buttonToggleForHide{...}
In interface build simply change the type from rounded rect to custom and then specify a new background image.
In code:
UIButton* b = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
Set the background image on it, set Type=Custom in interface builder.
Do you mean display button with custom image? You can try this:
UIButton *button = [[UIButton alloc]initWithImage:[UIImage imageNamed:#"button.png"]];
[button release];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = urRequiredFrame;
[button setImage:yourCustomImage forState:UIControlStateNormal];
button addTarget:self action:#selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubView:button];
#shabbir 1st select the button then press cmd+1 change the button type to custom
if it right select my answer
other way is that
UIButton* b = [UIButton buttonWithType:UIButtonTypeCustom];
And if you want to call image on button then use this
UIButton *button = [[UIButton alloc]initWithImage:[UIImage imageNamed:#"button.png"]];
[button release];

Problem in UIButton in iPhone programming

Can you give me some links or example programs to create UIButtons on a view and on action on it, we need to create one more button on other view..
plz help
-(void)action:(id)sender
{
}
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.frame = CGRectMake(x, y, width, height);
button.titleLabel.font = [UIFont systemFontOfSize: 30];
[button setTitle:#"TITLE"
forState:UIControlStateNormal];
[button addTarget:self
action:#selector(action:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];