How to modify UISwitchController so it ended up looking like this: - objective-c

Basically I want to customize the background and the button. I want the button to be square. Things like that.
My designers already provide me with this pictures:
A thing I am looking into is to just use UIButton. However, how do I make UIButton to be on switch mode? What should I do so that if button is clicked then it becomes "selected"
The result that I want is this:

This code may help Your query , you have to modify it ..
https://github.com/domesticcatsoftware/DCRoundSwitch
Thanks.

UIButton *switchButton = [[UIButton alloc] init];
[switchButton setBackgroundImage:offImage forState:UIControlStateNormal];
[switchButton setBackgroundImage:onImage forState:UIControlStateSelected];
When you want the button to look "on", just do: switchButton.selected = YES; after it has been tapped or whenever else you want to set it to that state.

Related

How to keep selected disabled button to look like selected button but dimmed?

I put this code to confirm my suspicion
if (self.Ihatethis.selected) {
self.Ilikethis.enabled=!self.Ilikethis.enabled;
return;
}
So basically one button will toggle enable state for the other.
Now I already have:
[self.Ilikethis setImage:[UIImage imageNamed:#"button-recommend-active"] forState:UIControlStateSelected];
[self.Ihatethis setImage:[UIImage imageNamed:#"button-dislike-active"] forState:UIControlStateSelected];
somewhere in viewDidLoad.
The result is when button is disabled, it looks like the normal button except that it's dimmed.
When button is disabled, I want the selected button to keep looking like it's selected state.
So how would I do so?
Don't disable your button. Make its userInteraction set to NO
btn.userInteractionEnabled = NO;
Hope it helps you..

UIBarButtonItem image (or UIToolbar custom background image?..) is darker (or lighter?..) than original

I want to create custom buttons for UIToolbar (and a custom background color for UIToolbar) in XCode.
So far, I've done it like this: create UIButton, then set an image to it and then drag it to UIToolbar. It becomes UIBarButtonItem after this, but still it has an image so it's fine for me.
The problem is, that for some reason buttons' images became darker than original images (I can see it because my toolbar background is exactly the same as buttons' background), but when I press the button, I see that its image becomes original-like and then after a sec turns back to the darker one.
I've tried to do as said in this question: Image in UIButton is darker tharn original, setting highlighted image in XCode, but after this it stopped 'changing to original' at all.
So, how can I solve this? I don't really want to bring some GUI stuff like this in code if there's a way to do this by XCode... Any help would be appreciated.
EDIT:
That's the screen shot of what's going on:
And that's how they should look like with toolbar background + buttom image combined:
Now I did it in code like this:
#implementation UIBarButtonItem (CustomView)
+ (UIBarButtonItem*) barItemWithImage:(UIImage *)image target:(id)target action:(SEL)action
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* item = [[self alloc] initWithCustomView:button];
return item;
}
#end
And the calling code:
UIBarButtonItem *item =[UIBarButtonItem barItemWithImage:[UIImage imageNamed:#"settings"] target:nil action:nil];
[self.bottomBar setItems:#[item]];
For toolbar background I use this code:
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:#"toolbar_background"]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
The problem remains the same...
EDIT 2:
Wow, it seems like actually the UIToolbar is lighter than original...
EDIT 3:
Well. Created new project and tested the XCode storyboard-way (with UIButton), there is works fine... That's a mystery for me, totally lost here. Need some serious clarification here about possible reasons of this: my current project is huge and I'm not able to post all the code here...
Well, since nobody answered, I'll just say that I created a new project and the problem gone.
But it happened once again, and I totally don't know why. I did a really lazy and bad thing and changed image brightness for that unlucky button in some image editor =D

Best way to create dynamic list of links/buttons in iOS5 view controller

I want to create a dynamic set of links/buttons in an iOS5 view controller and am trying to figure out what might be the best way to do this.
For eg:
Item 1
Item 2
Item 3
:
:
Item N
Each of the Items is a link/button that is clickable and will do some action, like load another screen etc based on the link.
I don't know ahead of time how many items there might be so if all the items don't fit on the screen, I need to be able to scroll to view.
My question:
1. What is a better way of doing this? I could just create the labels and buttons dynamically but this seems rather cumbersome and I'm not entirely sure how I would differentiate between the different buttons (essentially I'd need some index to find out which Item was clicked).
2. Alternatively, I was wondering if I can just render this page as HTML and just have links? I've never done this and not sure how I'd associate a button with a link.
Any suggestions?
AK
You can try to use the tag property to store the index value you need when you create the button. Then evaluate it in the button tap handler by accessing using button.tag.
Maybe you can try Cordova for an HTML based approach. I'm not too familiar with it though, so I can't say for sure.
Hope it helps.
(1) You can assign UIButton tag property based on the button index. If any events were to trigger, you could recognize which button the event belongs to by checking the tag.
Sample :
// Initializing some buttons
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.tag = 1;
[button1 addTarget:self
action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.tag = 2;
[button2 addTarget:self
action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
Selector for button events :
- (void)buttonPushed:(id)sender {
...
if ([sender tag] == 1) {
// do something after button1 event
} else if () {
// do something after button2 event
}
...
}
(2) If you choose to do it in HTML, you could check out CMHTMLView

UIButton's target is fired before animation

I have programmed a Quiz in Xcode where I've created UIButtons programmatically displaying the answer to a question. When the user clicks the button the next question is loaded and the buttons position i shuffled to not make the right answer in the same place for each question (the right answer is however under the same UIButton for every question.)
This is how I create my buttons:
if (self.buttonRightAnswer == nil)
{
self.buttonRightAnswer = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.buttonRightAnswer addTarget:self action:#selector(rightAnswer) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:self.buttonRightAnswer];
}
self.buttonRightAnswer.frame = CGRectMake(20.0, [[randomPositionArray objectAtIndex:0] floatValue], 280.0, 40.0);
[self.buttonRightAnswer setTitle:[[self.quiz.arrayWithQuestions objectAtIndex:questionNr - 1]valueForKey:#"RightAnswer"] forState:UIControlStateNormal];
When the user clicks the right an answer the next question loads. Here it's a problem. The next question loads with the same method only shuffled positions and the animation of the press-down of the button is drawn after the buttons been moved resulting in the user seeing where the right answer is now.
How can I wait for the animation before moving the buttons?
I could solve this by having the buttons stay in one place, shuffling out the questions over the buttons and comparing the users answer with the right answer.. but this requires a hell of a lot more code.
You can use some of shit-code and insert some kind of 'sleep' before next shuffling buttons
Before you create the buttons againg (after a question was answered), you can add something like (if you use differen images for button states)
-(void)waitUntilButtonDeselected
{
bool selected = YES;
while (selected)
{
bool selected = (self.buttonRightAnswer.currentBackgroundImage == UIImageOfNotSelectedButton);
}
}
The idea here is to check if the image of the button corresponds to not selected button.
Typically when using a UIButton, events are triggered on UIControlEventTouchUpInside, or at the end of the touch.
You're triggering it on the touch down which is causing issues with the animation you want to happen.
It might be simpler for you to switch to using UIControlEventTouchUpInside instead of UIControlEventTouchDown since this should solve your problem and be more consistent with other iPhone apps.

How to set the button image for UIControlStateSelected

Hi I want to change the background of my button after I press the button i.e. the button should not change back to the previous image.
However, I am not able to do so. My code as follows. Can anyone advise how I can change the image to "custom_button_highlight" after I click on the button?
UIImage *buttonImageNormal = [UIImage imageNamed:#"custom_button"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
UIImage *buttonImagePressed = [UIImage imageNamed:#"custom_button_highlight"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
button.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[button setBackgroundImage:stretchableButtonImageNormal
forState:UIControlStateNormal];
[button setBackgroundImage:stretchableButtonImagePressed
forState:UIControlStateHighlighted];
[button setBackgroundImage:stretchableButtonImagePressed
forState:UIControlStateSelected];
METHOD1
I see that you are using code to generate UIButton. It's easier to use Interface builder. Try this - In Interface builder (Xcode4), create the button & open the right sidebar. After that select the button, & the type of button as custom. After that in state config, there are various states, for each state you can set an image in image. Hope this helps...
METHOD2
If you want to use code to generate UIButton (it beats me why) then you seem to be doing the right thing. Are you sure the images are there in your bundle? You did not include the image extension (like .jpg etc.)
You mentioned, that you don't want the image to change back...
then instead of setting it for the states highlighted or selected, you have to set it for the state UIControlStateNormal.
But since you need it after it's been pressed, you have to catch the event that your button has been pressed and set the background image in there.
Hope it helped ;)
[_buttonConfirm setImage:[UIImage imageNamed:#"s.png"] forState:UIControlStateSelected ];
... does what you want programatically, not the background image. But I think you should follow Srikar's answer and use interface builder if you can.