button on a map is not getting displayed? - ios

In my app, I have a map view, I want to display a custom button on the map, if a place the button, it gets displayed but, if I place a background image for the image, it is not getting printed.
UIButton *customButton = [UIButton buttonWithType: UIButtonTypeCustom];
[customButton setBackgroundImage:[UIImage imageNamed:
#"Button_Default.png"]
forState:UIControlStateNormal];
[customButton setBackgroundImage:[UIImage imageNamed:
#"Button_Highlighted.png"]
forState:UIControlStateHighlighted];
[customButton setFrame:CGRectMake(60, 100, 200, 40)];
[self.view addSubview:customButton];
I have also tried changing the last line as
[mapView addSubview:customButton];

You have to make sure that you have the images added to your project, may be in resource folder & the file name you are using in your code should not be misspelled (quiet often people do typo mistakes like using keyword imagedNamed instead of imageNamed).

Related

UIButton programatically is not working in iOS

When I click on the UIButton nothing happened
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
You have to create button like below.
UIButton *checkbox = [UIButton buttonWithType:UIButtonTypeCustom]; // this is important
checkbox.frame = CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
You have to create the button first using button property and then give its frame.
If still its not working then self.background addSubview should be self.view addSubview.
What is self.background?
Your code is perfectly fine. You need to check the following .
check whether self.background is placed properly in your view heirarchy, I tried your code with self.view instead of self.background like below and it works perfectly.
self.checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 50, 50)];
[self.checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox1.png"] forState:UIControlStateNormal];
[self.checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox2.png"] forState:UIControlStateSelected];
[self.checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.checkbox];
From the error message that you have given
"
Warning: Attempt to present <enter_the_den: 0x7fbf1bd4c4e0> on <ViewController: 0x7fbf1bc771c0> whose view is not in the window hierarchy!
I suspect the issue is in your code inside the method autologin, just comment all the code in your method and just try a log , it should work.
refer this : whose view is not in the window hierarchy
If you read the Apple Documentation for UIButton and look under the heading marked as Creating Button you'll find that the way to create a UIButton is to use buttonWithType: and not initWithFrame:.
Return Value
A newly created button.
Discussion
This method is a convenience constructor for creating button objects with specific configurations. If you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.
When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value.
The only time when you should use alloc/init with a UIButton is when you have subclassed UIButton however your init method should then implement the buttonWithType: and pass in UIButtonTypeCustom.
So change:
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
To
// Create a button of type custom
checkbox = [UIButton buttoWithType:UIButtonTypeCustom];
// Now set the frame for your button
[checkbox setFrame:CGRectMake(73, 324, 15, 15)];
// Continue with the rest of your button code
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
Check userInteractionEnabled property of your background property.
But i see why you have no feedback on button if rest is fine - you use UIControlStateSelected for setting image when button has touch, but you must use UIControlStateHighlighted

How to programmatically set my UIButton image to 'redraw'?

I am using some vector graphics (.png) for my app. In photoshop, I can zoom in and always see a perfectly crisp boundary for simple geometric shapes, like circles. However, on my iPhone, my same graphics look jaggedy and rough. I found that if I change the image fill type to redraw', this helps the problem.
So, can someone please confirm if using redraw is the correct solution in this situation? Also, how can I make the image for a programmatically defined button use 'redraw' instead of the default (which I think is 'fill')?
Here is my current button's code:
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn1 setTitle:#"Cool title" forState:UIControlStateNormal];
[btn1 setFrame:CGRectMake(7, 7, 150, 160)];
[btn1 setImage:[UIImage imageNamed:#"myGraphic.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(selectFav) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn1];
remove this line:
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
add this line:
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
set content mode:
[btn1 setContentMode:UIViewContentModeRedraw];
Hope this answers all your concerns!

limit the touchable area of UIbutton

This is how I set button navigation bar
UIButton *addEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addEditButton setImage:[UIImage imageNamed:#"edit.png"] forState:UIControlStateNormal];
[addEditButton setFrame:CGRectMake(0, 0, 62, 31)]; used frame same as image
[addEditButton addTarget:self action:#selector(EditTable) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *addEdit = [[UIBarButtonItem alloc] initWithCustomView:addEditButton];
self.navigationItem.leftBarButtonItem =addEdit;
Everything works perfectly but button get pressed when I touch out side of it. How to solve this is there any way so it get pressed only if I touch on it
This is that image
apple has set this thing in way so user can navigate smoothly. it is advisable not to make such design in which you are putting buttons near navigationBarButton. there are some way to do it but its not good to change this kind of things. its just like reply to message and delete message both button are near beside to each other
I believe that you got this issue because the image you are setting is smaller thant the actual size of the button. please either make the button size smaller or provide a bigger image.
I hope this helps you.
It seems it works like that only, still I came up with a solution.
One is you can hide your navigation bar and use a toolbar indeed.
Other is you can add another button after that and set it's enabled property to FALSE.
I don't know whether this is proper or not, but seems to fulfill your requirement at least.
Here is the code:
UIButton *addEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addEditButton setFrame:CGRectMake(0, 0, 62, 31)];
[addEditButton setImage:[UIImage imageNamed:#"edit.png"] forState:UIControlStateNormal];
[addEditButton addTarget:self action:#selector(EditTable) forControlEvents:UIControlEventTouchUpInside];
UIButton *addEditButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[addEditButton1 setFrame:CGRectMake(63, 0, 30, 31)];
UIBarButtonItem *addEdit = [[UIBarButtonItem alloc] initWithCustomView:addEditButton];
UIBarButtonItem *addEdit1 = [[UIBarButtonItem alloc] initWithCustomView:addEditButton1];
addEdit1.enabled = FALSE;
NSMutableArray *buttonArray=[[NSMutableArray alloc]initWithCapacity:2];
[buttonArray addObject:addEdit];
[buttonArray addObject:addEdit1];
self.navigationItem.leftBarButtonItems =buttonArray;

Subclassing UIView/UIViewController

I need a lot of custom buttons in my app. At the moment I'm using storyboard to define these buttons on every controller that I need. However, I feel that since I need them throughout my app, I'm better off putting in a different view controller/view that subclasses UIView or UIViewController so if I need to make any changes I will just have to do them once as opposed to in different view controllers. What would be the best way to do this? Also can you tell me how can I create buttons programatically? This is what I'm doing at the moment, and I'm getting a completely blank screen.
-(void)viewDidAppear:(BOOL)animated
{
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
[testButton setFrame:CGRectMake(0, 390, 100, 40)];
[testButton setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
[setImage:[UIImage imageNamed:#"test_button"] forState:UIControlStateNormal];
[setImage:[UIImage imageNamed:#"test_button"] forState:UIControlStateHighlighted];
[setEnabled:YES];
[self.view addSubview:testButton];
}
there is a chance that your button is way off the visible rect. for adding the button programmatically, you are doing it mostly right. also, images should have the file extension. since you are adding a customButton, if there are no images, then you wont see any button. Try adding a title to your button, which would show up even if there is no image added.
try the code below.
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]; //you must have a good reason to not add this line.
UIButton *testButton=[UIButton buttonWithType:UIButtonTypeCustom];
[testButton setFrame:CGRectMake(0, 20, 100, 40)];
[testButton setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
[testButton setImage:[UIImage imageNamed:#"test_button.png"] forState:UIControlStateNormal];
[testButton setImage:[UIImage imageNamed:#"test_button.png"] forState:UIControlStateHighlighted];
[testButton setEnabled:YES];
[self.view addSubview:testButton];
}

UIButton doesn't show image

I have a UITableViewCell that I'm adding five UIButtons to. I programmatically create the buttons, add an action, and an image. The buttons are placed in the correct spot, the action works, and the tag is set, but they are clear. The images do not show.
This is what I'm using to create one of the buttons and add them to the cell, this code is in the tableView:cellForRowAtIndexPath: :
if ([[self.compCompletion objectForKey:#"Key" isEqualToString:#"YES"]) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(11, 6, 21, 21);
button.tag = 1;
[button addTarget:self action:#selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"Comp-Completed.png"] forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:button];
}
else {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(11, 6, 21, 21);
button.tag = 1;
[button addTarget:self action:#selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"Comp-Uncompleted.png"] forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:button];
}
I've tried adding: [button setEnabled:YES];, [button setHidden:NO];, and [button setNeedsDislpay]; to the process with no luck. How do I get the button to display the image?
EDIT:
My image exists, the UIImage created, and I can pull the UIImage out of the UIButton and save it to a file. I've gone through each property of the UIButton that controls appearance and changed them. I also tried an empty UIButton of type UIButtonTypeRoundedRect and UIButtonTypeAddContact. I'm now believe that this has something to do with the UITableViewCell not liking UIButtons, or how I'm adding the UIButton to the UITableViewCell.
Make sure that you have checked your project as Target Membership when copying the files. It helped me solve the problem.
My mistake was dragging the images into an images.xcassets file that was part of a pod, NOT the main project. It showed fine in the storyboard, but did not appear in the app.
In case anyone is still looking for an answer: Once the image was in assets.xcassets then I needed to include it in the "copy bundle resources" areas of the Build Phases part of the Target. Then I also had to set a transparent color so that it wasn't obscured.
project->targets->build phases->copy bundle resources-> "+" -> "add other"->select the image in the finder.
Check whether your images exist and are imported. Also it looks to me that besides of the image name the code is identical. A bit cleaner as:
UIImage *buttonImage;
if ([[self.compCompletion objectForKey:#"Key" isEqualToString:#"YES"]) {
buttonImage = [UIImage imageNamed:#"Comp-Completed.png"];
} else {
buttonImage = [UIImage imageNamed:#"Comp-Uncompleted.png"];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(11, 6, 21, 21);
button.tag = 1;
[button addTarget:self action:#selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:button];
Can it definitely find the images? Are they included in the project?
Just to test, try using an image you've already used somewhere else instead of Comp-Completed.png - if that works, you know that the problem is that it's failing to find that file.
UIImage * normalImage = [UIImage imageNamed:#"Comp-Uncompleted.png"];
UIImage * selectedImage = [UIImage imageNamed:#"Comp-Completed.png"];
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
button.frame = CGRectMake(11, 6, 21, 21);
button.tag = 1;
[button addTarget:self action:#selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:normalImage forState:UIControlStateNormal];
[button setImage:selectedImage forState:UIControlStateSelected];
[cell.contentView addSubview:button];
if ([[self.compCompletion objectForKey:#"Key" isEqualToString:#"YES"]) {
button.selected = YES;
} else {
button.selected = NO;
}
I know it is an old post but I came across the same problem and it was due to isHidden and isUserInteractionEnabled set to values to hide the button.
You have to be careful when to set them to YES or NO.
In case anyone is looking for answer, double check if you have copied the image to Assets.xcassets in project directory not in pods directory.
Second I used render as original image in Assets.xcassets image properties

Resources