Can we add button on scroll view - ios

I want to select multiple images from gallery and want to upload but before uploading I want to add a cancel button which help's user in deleting the image/image's after selection if user wants to. So how can I add a cancel button on image view ? I've tried the below code for adding button but when I select the multiple image's or single image then this button is not visible.
UIImage *imgview1=(UIImage*)[UIImage imageNamed:#"overlay.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//[button addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setImage:imgview1 forState:UIControlStateNormal];
button.frame = CGRectMake(25, 25, 60, 60);
[_scrollView addSubview:button];
I want to make the User Interface of the page like this image!

Unfortunately I can't comment yet, but could you provide a bit more clarification?
My understanding from the question is that you would like to have a scroll view with many buttons in it (the buttons will be selectable images), and on those buttons you would like to add an (x) button like in the image that will respond to taps.
If that is your question then I think the simplest answer is to add another button as a subview of the image when it is selected. When that subview button is tapped you can respond in a method like below:
(void)xTapped:(UIButton *)xButton
{
UIButton *myImage = xButton.superview;
// then unselect the superview here
}

Related

Button doesn't stay pressed when in Collection View

I have an app with this code
_pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
_pushButton.frame = CGRectMake(15, 2, 74, 74);
[_pushButton addTarget:self
action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchDown];
[_pushButton addTarget:self
action:#selector(buttonReleased:)
forControlEvents:UIControlEventTouchUpInside];
[_pushButton setBackgroundImage:[UIImage imageNamed:#"1.png"] forState:UIControlStateNormal];
[_pushButton setBackgroundImage:[UIImage imageNamed:#"2.png"] forState:UIControlStateSelected];
The behavior is that when the button is pressed the image gets darker and the button stays in that state until the button is released. The buttonPushed is called when the button is pressed and the buttonReleased is called when it is released.
That is what I expected.
I have another application with exactly the same code, that I have copied from the first.
The behavior is different: when the button is pushed, the image gets darker but about a second later the button gets to its original state even if it is still pressed. Only the buttonPushed is called.
This is not what I need.
1) Why the two apps has a different behavior?
2) How can configure the button in the second application to have the same behavior of the first?
UPDATE
In the first app the button is created in a class which subclass
UIView
In the second app the button is created in a class which
subclass UIViewController
UPDATE 2
The button which doesn't stay pressed is in a subview of a UICollectionViewCell. Apparently, buttons have a different behaviour when in a cell of a collection view.

Changing a UIButton's image when it's tapped

I have a UIButton that I am programmatically creating and adding to a UITableViewCell.
I have successfully set it up so that if you tap the button and keep holding it down, it will change to the image that I have set for the "highlighted state", but this is not good enough.
When the user taps the button, I need it to completely change to the new image. If the user taps the button again, I want it to change back to the original image.
I want the image to change every time they tap. Right now it changes if they tap and keep holding it down, but it switches back to the original image as soon as they are done holding it down.
Here is the code that I have so far for my buttons:
UIImage *addFriendButtonImage = [UIImage imageNamed:#"SliderThumb-Normal-G"];
UIImage *addFriendButtonImageHighlighted = [UIImage imageNamed:#"SliderThumb-Normal"];
UIButton *addFriendButton = [[UIButton alloc]init];
addFriendButton.frame = CGRectMake(237, -10, 64, 64);
[addFriendButton setImage:addFriendButtonImage forState:UIControlStateNormal];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateHighlighted];
I also tried setting the state for the new image to "UIControlStateSelected" but all that does it make the original image a little darker. It doesn't even change to the new image, and once again it only shows it's effect if you are holding the button down.
Set the image for both the UIControlStateHighlighted and UIControlStateSelected states:
[addFriendButton setImage:addFriendButtonImage forState:UIControlStateNormal];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateHighlighted];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateSelected];
Then listen for UIControlEventTouchUpInside:
[addFriendButton addTarget:self action:#selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
Then update the selected state:
- (void)handleTouchUpInside:(UIButton *)sender {
sender.selected = !sender.selected;
}
What you want is to set new image with [addFriendButton setImage:someImage forState:UIControlStateNormal]; every time the user taps on addFriendButton (look for event UIControlEventTouchUpInside).
If you want you can still assign appropriate highlight image on [addFriendButton setImage:someImage_highlighted forState:UIControlStateHighlighted];
You need to create an IBAction method for your button. If you are using a storyboard to design your view controllers go to the respective view controller in the assistant editor (tap on the icon in the upper right corner of the Xcode window), CTRL+drag the a line from the button to the .h file of your view controller's class like this:
Select Action and enter a named for the method like touchUpInsideButton.
Repeat the same steps to create a property for your button. (But select Outlet instead of Action and give your button a name like myButton.) Now you should have these two lines in your .h file:
#property (strong, nonatomic) IBOutlet UIButton *myButton;
- (IBAction)touchUpInsideButton:(id)sender;
Now go to the .m file of your view controller. Search for the newly created action method - (IBAction)touchUpInsideButton:(id)sender and enter the following code to change the button's image when the user taps the button:
- (IBAction)touchUpInsideButton:(id)sender {
UIImage *myImage = [UIImage imageNamed:#"myImageName"];
[self.myButton setImage:myImage forState:UIControlStateNormal];
}
(You can put any code inside this method that is executed whenever the user taps the button.)
I would recommend this method if you are using a storyboard. If not please refer to #Corey's reply.

programmat button and storyboard button

I have one button and one search field in navigation bar and i added a new button by code from one class for all view.
The problem is: with the button view the other button and the search field added with storyboard are not more touchable and not working.
UIImage * buttonImage = [UIImage imageNamed:#"chat-notify.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(goChat)
forControlEvents:UIControlEventTouchDown];
//[button setTitle:#"1" forState:UIControlStateNormal];
button.frame = CGRectMake(280.0, 25.0, 30.0, 30.0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:button];
and then in the controller:
notificationViewController* notification = [[notificationViewController alloc]init];
//[notification methodA];
[notification makeButtonNotification];
[self.view addSubview:notification.view];
Does anyone know where is my mistake?
Thanks
Why are you adding the button surely having it there and changing it's hidden state would be easier then you can keep it all in the storyboard
UIControlEventTouchUpInside
is what you should use not "UIControlEventTouchDown"
TouchUpInside is usually the event where you trigger the action from a button. It's when the user is lifting up from the button after pressing it. You don't want to trigger the action on Touch Down as the use may move his finger off the button to cancel the press, which is standard and expected behaviour on iOS
UPDATE: Base on your comment to this answer perhaps you need to add the button to a specific point in your view heir achy. As in your storyboard views will be added as they are listed in that stroyboard starting from the top down, with the one on the bottom being the top view.
Try obviously making sure that views that you want to receive touches are not directly on top of each and that they do not overlap. Also try adding the programatically generated button above or below a specific view, rather than just making it the top subview in your main view which by the sounds of it isn't what you want.
As you're adding a button like this
[self.view addSubview:button];
it may be better to do it more like this
[self.view insertSubview:button aboveSubview:someOtherView]
To do this you'll probably need to hook up your "someOtherView" to an IBOutlet so you can reference it in your code. As it looks like you're most likely sticking your button over everything else, when you need to be more precise.

How do I place a button in a UIImage for determining different selections?

I have a UIView with a UIScrollView. I then added a gesture recognizer programmatically to the scrollView for left or right swipe.
I created 3 UIImageViews with a different UIImage each, say 1, 2 & 3.
Ive gotten it so each swipe moves the new UIImageView into the center of the view. But now I need to distinguish between the image tapped. The actual tap will kick off a server request and get the data but I must determine the UIImageView tapped because each UIImage is different and will present the user with the server response plus the tapped image.
Each UIImageView actually has a tag set; 1, 2 & 3. My question is, how do I add a UIButton/IBAction to it? I was thinking of adding a UIButton as "child" of each UIImageView but Im not sure it can be done and I dont know if its the best way to achieve what I want, which is to be able to distinguish which UIImageView was selected by the user.
I ended up using a button with a background image instead of the image with a button-like reactivity.
fourthButton = [UIButton buttonWithType:UIButtonTypeCustom];
[fourthButton addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventTouchDownRepeat];
[fourthButton setTitle:#"SomeCardButton" forState:UIControlStateNormal];
[fourthButton setImage:[UIImage imageNamed:#"MyCard-Value.png"] forState:UIControlStateNormal];
[fourthButton setFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];

UIView/UIButton coordinates flipped

I have a coreplot project. The class of the view in viewcontroller has been changed to CPTGraphHostingView.
After creating the graph (this works fine),
I am adding a button as a subview , this launches a popover
the button shown is inverted. I have the same control in different view controllers and it works fine. The Popover also appears fine.
Here is the code that creates and adds the UIButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(monthSelection)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Change Month" forState:UIControlStateNormal];
button.frame = CGRectMake(330, 650, 111, 40);
[self.view addSubview:button];
[here is a screenshot of whats happening. UIButton is suppose to show somewhere below the popover. What am i missing.
Do not add subviews to the graph hosting view. Instead, make other views siblings of the hosting view, i.e., add them as subviews of the same superview.

Resources