iOS shuffle icon like in the Music app - iphone

How do I get this effect in a UITableViewCell? The icon placement is just to the right of the label.
Also, is this shuffle icon something that is standard which I should use? I have a similar application where the user can shuffle through words, each word will play in random order if they press this.

You can use the UIKit Artwork Extractor [1] to extract all the apple standard icons and use them in your application. Apple has no problem with this (so far).
[1] https://github.com/0xced/UIKit-Artwork-Extractor/blob/master/README.md

(assuming u click that icon to shuffle the list)
its as simple as adding a custom button with that imageview as background , as subview to the cell.but set the frame accordingly like
UIButton *shuffbutton=[UIButton buttonWithType:UIButtonTypeCustom];
shuffbutton.frame=CGRectMake(100,5, 30, 30);
[shuffbutton setBackgroundImage:[UIImage imageNamed:#"shuffle_icon.png"] forState:UIControlStateNormal];
[shuffbutton addTarget:self action:#selector(shuffle:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:shuffbutton];

Related

How to make a UILabel work as a url that's clickable and has url effect (when getting clicked)?

I know there is three20 solution. But I don't want to use three20 just for that purpose. So programmingly, is there a solution for that?
Yes there is a solution. You can use the regular expression to detect particular kind of strings on touches began of the label and then based on the regular expression which matches the string you can perform the action.
So if you want to do that, you need to subclass the UILabel and overwrite the Touch methods for the label.
Hope this helps you.
There is one of the Label's called OHAttributedLabel which does similar function. And here is a fork, that supports ARC.
You can use a UIButton with the button type as UIButtonTypeCustom, on the button click you can call the url.
UIButton *urlButton = [UIButton buttonWithType:UIButtonTypeCustom];
urlButton.frame = CGRectMake(10, 10, 80, 30);
[urlButton setTitle:#"Click Here" forState:UIControlStateNormal];
[urlButton addTarget:self action:#selector(urlButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:urlButton];

UIButton setImage not working

I'm losing my mind over this one. I have UIButton connected as Outlet. I want to change it's image. In the viewDidLoad function, I try to set the image like so:
[button1 setImage:[UIImage imageNamed:#"house.png"] forState:UIControlStateNormal];
Nothing happens. However, if I use ImagePicker, it somehow works. In the ImagePickers function "didFinishPickingImage", I repeat the exactly same command as above for setting image and it works, the image is displayed normally.
I've been thinking that maybe the button's image is not refreshing itself, but trying to call [button1 setNeedsDisplay], or [button1.imageView setNeedsDisplay] or even [button1.imageView.image setNeedsDisplay] does nothing.
Does anyone have any idea about this? I've wasted two hours by now and I'm really getting frustrated, because I'm certain that the reason must be so stupid it's unbelivable.
Please check your button type.
If your button type is "System" you should change it to "Custom"
Two things to do here:
#1, change your code to:
[button1 setImage:[UIImage imageNamed:#"house.png"] forState:UIControlStateNormal];
and #2
make sure your UIImage returns a valid image.
In other words,
UIImage * imageToSet = [UIImage imageNamed: #"house.png"];
if(imageToSet)
{
[button1 setImage:[UIImage imageNamed:#"house.png"] forState:UIControlStateNormal];
} else {
// if you see this line in your console, then you know where to look for a problem
NSLog( #"why is my image object nil?");
}
For anyone who will find themselves in a similar situation; I was using the mentioned button inside a custom cell for table view. However, the cells aren't loaded right away (i.e. inside viewDidLoad/viewWillAppear etc.), but inside the tableView:cellForRowAtIndexPath function. All my button variables were nil at that point. My solution was to load the image after the cell has been created and it has worked straight away afterwards.
Here are a couple additional notes. UIButton setImage: forState: is the correct method:
[button1 setImage:[UIImage imageNamed:#"house.png"] forState:UIControlStateNormal];
imageNamed is a great convenience method for grabbing the image, but make sure it returns an object. Forgetting to include the image file in the current target gets me all of the time.
[UIImage imageNamed:#"house.png"]
You also don't need the extension. The SDK works this out.
[UIImage imageNamed:#"house"]
And even though UIControlState is a bit mask, you cannot OR the values together. This does not not not work and I must must must remember this because it gets me every time I use the call.
// NO NO NO
[button1 setImage:[UIImage imageNamed:#"house"] forState:UIControlStateNormal|UIControlStateSelected];
// YES
[button1 setImage:[UIImage imageNamed:#"house"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"house"] forState:UIControlStateSelected];
Was getting the same problem when UIbutton was in UITableViewCell.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"detail.png"] forState:UIControlStateNormal];
should work in UITableViewCell
Glad to know that the problem is solved but none of the answers actually addresses the problem.
Generally, if the code is correct, then the error shifts from "syntax error" to the "logical error" category. There is no definite guide to handle it but here is a small checklist that can help anyone to troubleshoot their code.
1) The IBOutlet is not connected or renamed.
2) Make sure that the UIButton type is custom.
3) The image is set (more than once), somewhere else in the code.
4) You are trying to set the Image through any sync method which is not using the main thread. (There are several threads on that alone).
5) If you are nesting the images as subviews, make sure the problem isn't with their parent view.
6) If it is appearing in Simulator but not in the device, It's a lettercase (Uppercase, lowercase difference in the code and the filename.
7) Modern simulators works with very low GPU memory on older machines. You can tweak the simlator options to make it appear. E.g (Command + Right Arrow) twice to change the simulator orientation and your changes will appear.
There are few other but common pitfalls where developers usually overlook
1) The image doesn't exist OR you are making a spelling mistake.
2) The filetype is different than mentioned. (JPG, PNG).
Last thing you'll want to check - make sure you're setting the image on the main thread! Just ran into an issue where I was setting a button image in a callback from ALAssetLibrary (fetching images from photo library). The image wouldn't update until I tapped the button. Turns out the callback was happening off main...
for those having issues with system images not appearing, just use systemName: instead of named
if let starImg = UIImage(systemName: "star.fill") {
button.setImage(starImg, for: .normal)
}
It's for ios13 and later...
For me it was to move the line:
[button1 setImage:[UIImage imageNamed:#"house.png"] forState:UIControlStateNormal];
from awakeFromNib method
to viewDidLoad method
I guess,
don't create like this
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
by this create method ,UIButton is create by use default type,so your background image is not work.
try this,
UIButton *nextButton = [[UIButton alloc] initWithFrame:CGRectMake(9, 154, 300, 48)];
or other,except buttonWithType
This is happening because the state of the Button doesn't stays selected once clicked by default.
I solved it by making an IBaction on that button click and toggling the selected state.
-(IBAction) checkButtonClicked:(id)sender{
checkButton.selected = !checkButton.selected;
}
In my case, it is drawn by custom animation code and not set by the image, so it doesn't work by setting any image, it has to change the animation code.
With images, you have to import them into the project not as references but as copies. So when you go to add files to project, choose "Create groups" instead of "Create folder references". Also make sure "Copy items if needed" is checked.
This is confusing because importing as references works when you are using images as textures (e.g. for Sprites). However, it doesn't work when you want to use images for buttons (e.g. for using image name in [UIImage imageNamed:#"house.png"]
I faced this same issue of the image not being shown on the UIButton.
I had to change this line -
myButton.setImage(<my_UIImage_object>, for: [.normal, .focused])
to this -
myButton.setImage(<my_UIImage_object>, for: .normal)

Custom UIButton iphone

I want to change the color of my UIButton. I did this:
UIButton *alreadySubscriber = [[UIButton alloc] initWithFrame:CGRectMake(12, maxy-65, 262.0, 50)];
alreadySubscriber.layer.cornerRadius = 10.0f;
[alreadySubscriber setBackgroundColor:[UIColor orangeColor]];
[alreadySubscriber setTitle:#"Already a subscriber" forState:UIControlStateNormal];
The color of the button changes, but no longer see the effect of bright light booby top. how can I fix this?
We're unable to programmatically set this 'bubble effect' for the UIButton I'm afraid. The only way to go about it far as I can tell is to go for a custom button with an image (or two images for the normal and active states).
The resource Eugene put up earlier seems pretty good in fact.
I don't really know what kind of booby light are you talking about, but have you tried using images for your buttons? I'd suggest using two different images like those you can create here http://dabuttonfactory.com/.
You should create as a custom button and add the image/color as background.

iphone: unable to change background of buttons

I have a situation I'm trying to change background image of 4 buttons like this:
if(some condition){
[firstSeverityButton setBackgroundImage:[UIImage imageNamed:#"greySeverity.jpg"] forState:UIControlStateDisabled];
[secondSeverityButton setBackgroundImage:[UIImage imageNamed:#"greySeverity.jpg"] forState:UIControlStateNormal];
[thirdSeverityButton setBackgroundImage:[UIImage imageNamed:#"greySeverity.jpg"] forState:UIControlStateNormal];
[fourthSeverityButton setBackgroundImage:[UIImage imageNamed:#"greySeverity.jpg"] forState:UIControlStateNormal];
[fifthSeverityButton setBackgroundImage:[UIImage imageNamed:#"redSeverity.png"] forState:UIControlStateNormal];
}
But the background of the other four disappears when I touch any one
Please enlighten me on this how can his be tackled.
May be you are looking for the radiobutton, You can write down a custom class for radio button and make things work out easily.
[firstSeverityButton setBackgroundImage:[UIImage imageNamed:#"greySeverity.jpg"] forState:UIControlStateDisabled];
This code actually works fine, with every control state. I think the problem is with the initial image for the button and the current button state (in both nib and code).
The state of buttons can be changed with:
button.enabled=yes;
button.highlighted = NO;
button.selected = NO;
When you setup image for control state specifically, the change on image appear on that specific control state. So the change depends on both the initial image and the current control state of button
NOTE: We are applying change on the background image. There is an image property for the button which can actually hide the changes of background image. If you setup the image
[firstSeverityButton setImage:[UIImage imageNamed:#"greySeverity.jpg"] forState:UIControlStateDisabled];
anywhere in code and maybe it can hide your background image setting.
Also there is a possibility of error in your if-else loop ...go through the loops and verify every conditions whether the images get set properly.

Inverting UIBarButtonItem "Play" for use as a Back Button?

Okay, simple enough.
I'm coding up a simple web view, and I'm providing a few Safari-like controls for navigation.
Play seems to be the obvious choice for the forward button, but I'd like to have a Back Button as well, as seen in several Apple and third party apps.
Is there a way to invert the icon, so that it points backwards, or are all the apps using this setup using images to replicate this functionality?
Unicode is your friend here.
Create a UIBarButtonItem (in Interface Builder or in code) and set it to "Custom" where you can enter text for the button.
Now use these Unicode characters to simulate the back & forward buttons:
◄ and ►
I use it in my apps and it looks great.
I know this was answered long ago, however just to add...
Yes, you can use Unicode but it does not look the same as the standard ios button. And if you have strict requirements to match ios look than you will need to at least make an image for the back button, in this case by simply downloading the play image and flipping it.
UIImage *image = [UIImage imageNamed:#"backButton.png"];
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack setImage:image forState:UIControlStateNormal];
btnBack.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
UIBarButtonItem * btnItem = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
[btnItem addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
// add it to your bar
self.navigationItem.leftBarButtonItem = btnItem;
(wrote it without spell checking... so beware of spelling errors that may have resulted)
// button images are here.
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/IconsImages/IconsImages.html
You could use atPeek to extract the play button png from an app that uses it, and then use any image manipulation program to flip it.