ios horizontal scroll menu - background buttons select - ios

I try to make an horizontal navigation menu in scrollView. For every items in the menu i had a different image. When i select an item, i would like to change background image of this item, but i don't know why my code doesn't work:
for (int i=0; i<12; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *normal=[UIImage imageNamed:#"icon1"];
UIImage *selec=[UIImage imageNamed:#"icon1down"];
[btn setFrame:CGRectMake(i*larg, 0, 42.0, 43.0)];
[btn setBackgroundImage:normal forState:UIControlStateNormal];
[btn setBackgroundImage:selec forState:UIControlStateSelected];
[scrollCat addSubview:btn];
cx+=42.0;
}
[scrollCat setContentSize:CGSizeMake(cx, 43)];
Is there a bad thing in this code?

Try UIControlStateHighlighted instead of UIControlStateSelected.
Highlighted is the state when the user touches your button.
Selected is only used for some controls, like segments ; a custom button (one that say, stays selected) would/could make use of Selected.
From the UIControl_Class docs at developer.apple.com (emphasis mine):
UIControlStateHighlighted
Highlighted state of a control. A control enters this state when a
touch enters and exits during tracking and when there is a touch up
event. ...
UIControlStateSelected
Selected state of a control. For many controls, this state has no
effect on behavior or appearance. But other subclasses (for example,
the UISegmentedControl class) may have different appearance depending
on their selected state. ...

Related

UIButton tint color affected the background color of the button in selected state

I have a button with typeSystem that has a specific image on normal state and a different one for a selected state. Problem is, when I set my button to selected state, the image won't show and the button's background is colored with the tint color.
Is there any way to disable it from happening and showing the image I set for selected state?
Instead of using buttonWithType:UIButtonTypeSystem you should replace it with self.yourButton = [UIButton buttonWithType:UIButtonTypeCustom]; this will forego the tinted selection option you are getting. Then you can alter it's states images as the following:
[self.yourButton setBackgroundImage:[UIImage imageNamed:#"unselectedImage.png"] forState:UIControlStateNormal];
[self.yourButton setBackgroundImage:[UIImage imageNamed:#"selectedImage.png"] forState:UIControlStateSelected];
Now the tint won't display and your selected/unselected images will retain that image until you alter the state again

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.

Can we add button on scroll view

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
}

UIButton changes states when being pressed

I have an UIButton configured with two states, default and selected, for different text and background, the set up is done on Interface Builder. The weird thing is when the button is in selected state, and the button is pressed, it automatically changes to default state. After the button is released, it changes back to selected state. Is there way to disable roll-back-to-default behavior?
Forgot to mention that, the type is custom.
Update
tried the following code in viewDidLoad
[self.button setTitle:#"default" forState:UIControlStateNormal];
[self.button setTitle:#"selected" forState:UIControlStateSelected];
[self.button setTitle:#"highlighted" forState:UIControlStateHighlighted];
[self.button setSelected:YES];
The button shows default when pressed.
Selected state and Highlighted state are not mutually exclusive. When you press a selected UIButton, it gets into a selected AND highlighted state.
I don't think there's a way to set that in the IB, but you can do this in code:
[button setImage:[UIImage imageNamed:#"some_image"] forState:UIControlStateSelected | UIControlStateHighlighted];

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.

Resources