Button with first half transparent and other half title - ios

I have some requirement to prepare a UI like Image 1:
But sometime i get it like(title disappeared)
I am not sure why this is happening.
The background image second has half transparent and half image(refresh image),there i need to show text.
EDIT
XIB settings:
Any idea or solution for this ?
OR
Please suggest some other way to achieve this.

I just found in the UIstoryboard, why don't you try in this way
You can configure this UIButton as your need with the title attributes, right ?

As per your comment => colour of the title will be configured from server.
Then may be it is also possible that sometimes you will get clearColor of your button's title from server so you not able to display title of button. For solve this issue you need to put condition such like,
if(ButtonTitleColor is "clearColor")
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // here set color whatever you want.

Related

Weird appearance about button selected

I'm sure I set right images for different states. But the UIButtons looks not good. Like this:
Anyone knows why? Many thanks in advance!
change from system to Custom then try ,
[UIButton buttonWithType:UIButtonTypeCustom];
I didn't enter any words(including while space) in all states.I made tint color white.It worked.And I think maybe this is caused by auto layout.

How to set the text color of the UISearchBar's placeholder

I have a UISearchBar with a dark background color so I was trying to change the place holder text color of UISearchBar (which will be gray by default) but I didn't find a way to set it. So I thought of getting some help :) please suggest me how this can be achieved thanks in advance :)
Note: At the time I wrote this answer I was working on iOS 7 & this workaround worked on iOS 8 as well. It may not work on iOS 9.
Ok it's been two days since I posted this question. Though I din't get the answer I got a workaround for this problem.
Note : I am using storyboard for my application & I have subclassed the UISearchBar and this workaround working like a gem for me.
First and foremost add an appearance proxy to UILabel when its contained in the UISearch bar class like this :
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];//change the color to whichever color needed
Then the most important thing is If you're using storyboard you must and should write some text in the placeholder field of the attribute inspector like this
If you forget to write something in the placeholder field, definitely this trick will not work when you run the application and the placeholder text will look in usual gray color.
Once you're done with writing some text in placeholder field, set the place holder text in your UISearchBar sub class like this :
self.placeholder = #"My Placeholder text";
Now run the application !!! You'll get the place holder text in the color which you have set in appdelegate:) Hope this helps someone :)
Here is the solution
[searchBar setValue:[UIColor blueColor] forKeyPath:#"_searchField.textColor"];

Displaying image in UIButton

To preface this question, I am extremely new to Objective-C and Xcode. I have only been learning for a week or so now but I have been stuck on this problem for a few days. I am trying to display an image in a UIButton. I have 3 to choose from, blue.png, purple.png and orange.png. No matter what I do or what image I tell Xcode to use, it will only display the blue image. I have deleted the image from the project entirely. I have "reset content and settings" in the iOS simulator. What exactly is going on here?
the.h code
IBOutlet UIButton *h1;
the.m code
UIImage *buttonImage = [UIImage imageNamed:#"purple.png"];
[h1 setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:h1];
No matter which image I specify within the quotes, it always uses blue.png, even when the image no longer exists in the project. Thanks for the help everybody!
You must have set blue.png as the background image in the storyboard or xib file. Write NSLog(#"%#",buttonImage) to make sure that the image is not nil. Make sure that you are entering the correct name and extension purple.png
And also, use
[h1 setBackgroundImage:buttonImage forState:UIControlStateNormal];
instead of setImage
I think first of all you should know the difference button.image and button.backgroundImage.
Using the Image (currentImage) field, you can specify an image to
appear within the content of your button. If the button has a title,
this image appears to the left of it, and centered otherwise.
The Background (currentBackgroundImage) field allows you to specify an
image to appear behind button content and fill the entire frame of the
button.
So you can use setImage or setBackgroundImage as what you want.

How do I change an Image when a button is pressed?

Now I know this question may sound like a duplicate, but I doubt it seeing as I've researched and none of the possible duplicates answer my question. If it is a duplicate and it's been answered by all means show me the way!
But my question doesn't deal so much with a button changing an image, more so the button is the image.
So my button is an image and when pressed a sound plays and then the image changes until the sound is done. My problem is I can't get the image to change.
Now I have tried this method thinking that I could press my button and my new image would cover up my button until the sound was done:
UIImage *dolDerp = [UIImage imageNamed:#"dolphin2.png"];
[imageview setImage:dolDerp];
I have an outlet set up and it's connected to an image view object and then the action is connected to the button, so in theory when the button is pressed the new image should take over the screen. Now obviously the code needs to be tweaked so the button would go away after a few seconds when the sound is played, but my problem is I can't even get the button to display. I also would prefer to just have the button objects image change if possible?
If anyone could offer some help it's much appreciated!
the best thing is in Interface Builder assign the two images to the button Normal/Selected states.
in code just use this line:
myButton.selected = YES;
//or
myButton.selected = NO;
What #Mahmoud Fayez suggested is good. In code, the proper way to set a buttons image/background image is [button setImage:image forState:UIControlStateNormal] or the similar method for setBackgroundImage
If you are using IB though, it is indeed best to set the different images for different states in IB, then change the buttons state (which will then change the buttons image) in code.

UIButton Highlighting Overcome default behaviour

In a program much like the who want to be a millionaire... I have 4 option buttons. It should flash red or green on press. The four buttons have either red or green images as background images. In the code setup I have such a method ready to setup the first question and when the second question gets set, the background images change accordingly.
Currently the default blue highlight is always appearing on the first pressing of any one the buttons and after that there are other issues.
I could paste some different approaches here, (but they dont work). So any suggestions would be good.
Also in the IB, when you set the highlight with background image, that works. I would use use, but for the same button, the back ground image must be changed programmatically.
Thanks in advance for the help/suggestions!
Use two State UIControlStateNormal and UIControlStateHighlighted
[btnMenu setBackgroundImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
[btnMenu setBackgroundImage:[UIImage imageNamed:#"img1-Highlighted.png"] forState:UIControlStateHighlighted];

Resources