Using the following code to customize regular UIButtons also affects UIBarButtonItems and clear buttons in text fields.
[[UIButton appearance] setBackgroundImage:greenButtonImage forState:UIControlStateNormal];
I do not wish to customize the latter elements at all, only regular round rect buttons. I realize using appearanceWhenContainedIn: could be used to set a custom appearance for UIBarButtonItems and UITextField, but i wish for these buttons to remain standard. Subclassing is not an option here as it should not be required for such a simple task.
There is a similar question, but it does not address the issue. Why does -[[UIButton appearance] setBackgroundImage] affect the initial appearance of UIBarItem objects and how do you correct it?
One solution I've used before is to nil out the "backgroundImage" property for UIButtons contained inside of a UINavigationBar:
[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setBackgroundImage:nil forState:UIControlStateNormal];
This should allow you to customize UIButtons in other cases, without touching the ones inside a UIBarButtonItem in the UINavigationBar.
Related
iOS uses standard (blue) tint color for all colored texts in MFMailComposeViewController. This is not good for me, as customer wants his company colors in app. How to change their color to orange?
I am asking specifically about colors of button icons (add image and bell image) and texts containing mail addresses. I already have navigation bar colors changed. In documentation, there is written:
The view hierarchy of this class is private and you must not modify it. You can, however, customize the appearance of an instance by using the UIAppearance protocol.
I have tried to use it, but it is not working (I might doing it a wrong way, as I do not know UIAppearance). This is what I have tried:
[[UIButton appearanceWhenContainedIn:[MFMailComposeViewController class], nil] setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[[UILabel appearanceWhenContainedIn:[MFMailComposeViewController class], nil] setTextColor:[UIColor orangeColor]];
As Apple says: https://developer.apple.com/library/ios/documentation/MessageUI/Reference/MFMailComposeViewController_class/index.html
The view hierarchy of this class is private and you must not modify
it. You can, however, customize the appearance of an instance by using
the UIAppearance protocol.
Anyway, you can check this post:
Customizing automatic MFMailComposeViewController opened from UITextView
I got this problem too. Just use:
UIView.appearance().tintColor = .orange
This works fine but there is a flaw. The recipients text will change back to system tintColor(blue) when editing.
I've observed following:
By setting the Titlecolor of a UIButton with appearance, the UIMenuItems in a UIMenuController of a UITextView are getting the same color.
Code in applicationDidFinishLaunching:
[[UIButton appearance] setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
My question:
Is there a way to suppress it
or
give a UIMenuItems another color?
What i have tried:
With appearanceWhenContainedIn UITextview
I've tried to set the appearance for buttons contained in TextViews with
[UIButton appearanceWhenContainedIn:[UITextView class], nil]
But this obviously didn't work since the UIMenuController is not inside the TextView.
With appearanceWhenContainedIn UIMenuController/UIMenuItem
Is not possible, since both are not implementing the UIAppearanceContainer protocol.
I found 2 ways to fix this issue.
Here is a screenshot of the result of the following solutions :
First solution
The UIMenuController is not contained in the View Controller views hierarchy. You can thus define your UIButton color that way (instead of setting the global Button appearance) :
Swift :
UIButton.appearanceWhenContainedInInstancesOfClasses([UIViewController.self]).setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
Objective-C :
[[UIButton appearanceWhenContainedInInstancesOfClasses:#[UIViewController.class]] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
That solution works for most of the cases. But if you use the delete button or actions button of Table View Cells it will also take the set color and you won't be able to change that color through appearance proxy.
Second solution (my preferred one)
The second solution uses directly the private UIButton subclass class name used by Apple in the Menu Controller.
I would never recommend to access a private Apple class (and furthermore through its name), but in that specific Menu Controller color customization case I think that's the best solution. It lets you define the clean way your view appearances.
Swift :
Define your global Button title color appearance :
UIButton.appearance().setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
Specific exception for the MenuController :
(NSClassFromString("UICalloutBarButton")! as! UIButton.Type).appearance().setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
Objective-C :
Define your global Button title color appearance :
[[UIButton appearance] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
Specific exception for the MenuController :
[[NSClassFromString(#"UICalloutBarButton") appearance] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
I was wondering how you would hide an image in Objective-C.
I know you can hide text or a button like this
StartGame.hidden = YES;
But when I try to do this with an image, i get an error.
Since UILabel, UIButton, UIControl and UIImageView are all childs from UIView they all have the hidden property to hide them.
So any instance of the of a view (or its childs) can be hidden by setting the hidden property to YES.
If your UIImageView is part of your viewcontroller it would make sense to make a property of it rather then just an variable, but I can not see your full code or error to say that the error might be found there.
Also property or variables should not start with a capital, its not in line with the Objective-C style.
UIImageView *ImageView=[[UIImageView alloc] initWithFrame:CGRectMake(x, y,width, height)];
[ImageView setImage:[UIImage imageNamed:#"image.png"]];
and to hide this image You just need to write
[ImageView setHidden:YES];
There is a Highlight Tint option in Interface Builder for UIButton. Is it possible to change it programmatically for all UIButton in iOS 5....using some kind of appearance protocol thing or some other workaround?
You can set it as
[button setTintColor:[UIColor grayColor]];
This is equivalent to hightlight tint option in IB and is applied only for highlighted state.
Update:
In order to implement this for all the buttons in app, use this:
[[UIButton appearance] setTintColor:[UIColor orangeColor]];
It will set for all the UIButton which you are going to use in your app.
Check this for more details on UIAppearance protocol.
I'm currently setting menu items to change the colours of "individual" windows in my Mac application.
[_window setBackgroundColor : [NSColor redColor]];
Is there a way to make that menu item change in all my windows, in contrast to just "_window"?
You could use the windows method of NSApplication.
This would probably do it:
[[NSApp windows] makeObjectsPerformSelector:#selector(setBackgroundColor:) withObject:[NSColor redColor]];
(Or you might want a for-loop to examine each window more carefully.)