UIButton title text color change - ios

I have a UIButton in a custom UIView. In the view, I have a function setDisabled:(BOOL)disabled which is called when I want to change the appearance of the UIView.
\\ Method 1
- (void) setDisabled:(BOOL)disabled
{
myUIButton.titleLabel.textColor = disabled ? [UIColor systemGrayColor] : [UIColor systemBlueColor];
}
\\ Method 2
- (void) setDisabled:(BOOL)disabled
{
[myUIButton setTitleColor: disabled ? [UIColor systemGrayColor] : [UIColor systemBlueColor] forState: UIControlStateNormal];
}
I have used this UIView for cells in UITable view. Using Method 1, the change is not immediately reflected (reflected when i scroll through UITableView) whereas using Method 2, the color change is immediately reflected. In this apple documentation, it is mentioned to not use Method 1. However I could not find the reason. Any leads would be helpful.

Do NOT use "Method 1" ...
UIKit controls - such as UIButton - have a lot of code that does work "behind the scenes."
For example, if your button has a normal color of Red, and a Highlighted color of LightGray, and then you execute:
myUIButton.titleLabel.textColor = [UIColor systemBlueColor];
The button itself does not know that you did that. It still has Red as its normal color property.
As soon as UIKit updates that button - a tap or a layout change, etc - the text color will be set back to the normal color property.

Related

tvos UISegmentedControl focus style not change

I want to change background color of UISegmentedControl when it highlighted in tvOS.
Normally Segment display like following.
When change focus for change selected segment at that time display like following.
How to change white background when UISegmentedControl focused?
I was try following things but not working.
1) create custom class of UISegmentedControl and do following code in awakeFromNib
[self setBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:image forState:UIControlStateFocused barMetrics:UIBarMetricsDefault];
2) override setHighlighted method of UISegmentedControl
3) change background color in didUpdateFocusInContext method.
From my understanding of your question, you want to change the background color same as UISegmentControl tint color. What you need to do is simply use this:
label.backgroundColor = segmentedControl.tintColor;
Hope this will help you if not try this:
You just need to give its background color to clear from storyboard. This is happening due to default background selection and default selection is as per theme. Once you set the segmentControl background color to clear you will get the required result.
Or you can do:
Try to set your UISegmentcontrol background color to clear in viewDidLoad, you will get your result. I have tried this and it worked for me.
segment.backgroundColor = Color.clear;
its easy to clear the background color of segment control:
yourSegment.backgroundColor = .clear

TintColor Changing on Popover Push

I'm setting the tint color of a window to an arbitrary color, then trying to over-ride this on a per-button basis, but it appears that the buttons revert to the window tint color whenever there is a segue applied on them.
Setting tint color in didFinishLaunchingWithOptions:
self.window.tintColor = [UIColor redColor];
and then my two buttons in viewDidLoad:
[self.button1 setImage:[[UIImage imageNamed:#"711-trash"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
self.button1.tintColor = [UIColor purpleColor];
self.button2.tintColor = [UIColor blueColor];
where button1 is a custom type button and button2 is a system type button.
When the popover first presents, the two buttons are tinted purple and blue. But when the segue view controller is pushed, the popped, the two buttons switch to red. Is there any way to prevent this?
EDIT:
I've tried reproducing your code like this:
However everything worked as expected:
So I agree with #user3779315, possibly you are setting the buttons' tint color somewhere else. Btw, additional code of your project would help to clarify the issue :-)

Button label color change doesn't work if font is also changed

Here is a simple example custom button that is set as a class of a button in IB:
#import "TestButton.h"
#implementation TestButton
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
UIFont * font = [UIFont systemFontOfSize:8];
if ( self.setFont )
self.titleLabel.font = font;
self.titleLabel.textColor = [UIColor redColor];
}
#end
If setFont is false, that is, the font is unchanged, the the label text color is red as expected. But if it is true, the button text color is whatever it is set to in IB.
So the question is what's going on here, and how can I change both the text and the font of a button that is assigned in IB programmatically.
In case someone wants to see these peculiarities and also how funky IBDesignable can be, see demo project
Replace
self.titleLabel.textColor = [UIColor redColor];
with
[self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
The below code is for setting text:
[self setTitle:#"New Text" forState:UIControlStateNormal];
If you wanted to set the title or the color of the title anywhere other than within the button file itself, you could try adding two public properties to the button. One NSString property for the title (self.titleString) and one UIColor property for the title color. You could then set the title and or title color anywhere like in your view controller or view. You would then use these properties within your button file like
[self setTitle:self.titleString forState:UIControlStateNormal];

Change the background color of a button

I have code that counts upwards when buttons on my app are pressed, but I'd also like to change the button's background color once it has been tapped.
Can I simply add a backgroundColor to the below code?
- (IBAction)buttonPressed {
count++;
totalLabel.text = [NSString stringWithFormat:#"Hours\n%li",(long)count];
}
#end
Sure. The easiest thing to do is change your IBAction so it receives the button that's been tapped.
-(IBAction)buttonPressed:(UIButton*)button {
button.backgroundColor = [UIColor redColor];
}
(You'll need to reconnect the outlet from the nib as well so it knows that the new selector is buttonPressed:, not just buttonPressed.

UIButton adjustsImageWhenHighlighted Property With Background Color

I have a set of buttons on a view controller that are added to the .xib and their properties are adjusted programmatically.
The background color is set in code as follows and then when it is tapped I change the background color using a selector method.
in configureButtons method:
[btn addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchDown];
which calls the following:
-(void) changeButtonBackGroundColor:(id) sender {
[sender setBackgroundColor:[UIColor colorWithRed: 199/255. green: 95/255. blue: 45/255. alpha:1.0]];
}
Adjusting the background when touched programmatically works fine when the "Highlight Adjusts Image" property is set to YES but it shows the white glow when touched as well as changing the background color.
I don't want to see the white highlight when the button is touched so I turned off the property. This causes the background color change to stop working.
Is there a way to change the background color of a UIButton without also showing the white glowing highlight when touched?
You can try using background images with particular color. In that case you can easily make use of – setBackgroundImage:forState: function. I think this will work even when showsTouchWhenHighlighted is set to NO.

Resources