Override [UIButton Appearance] for one button - objective-c

See subject. Is this possible? When I add a tint color with UIButton Appearance... it applies to all UIButtons, not just "System" buttons but even custom ones.
I just want to be able to set a couple of destructive buttons with red text while keeping a customizable tint color for everything else.

Check out UIAppearance appearanceWhenContainedIn.
This one provides way to customize appearance whenever your control is contained within specific view, such as UIToolbar or UINavigationBar. You may check up your own parent view class which contains your custom UIButton.

Related

Background property of UIButton

I can see that there is a Background property in the Attributes Inspector of UIButton but how do I set it programmatically?
It is not background color or background image because I have tried setting these in code but it doesn't have the desired effect.
I used setBackgroundImage of UIButton
First, be sure to have a referencing outlet for your button then,
try, button.backgroundColor = UIColor.black of course the 'button' name may be diffrent for you and the color of choice may also be.
If this doesn't work for you for some reason this video may be of help: https://www.youtube.com/watch?v=9Vq_xckdJkQ

Objective-C UIButton in UIView Hierarchy not animating

I have a ScrollView with multiple Subviews and somewhere down there two UIButtons. They are not covered by a different UIView and they are perfectly visible.
The problem now is that the buttons are working, but they are not getting the "pressed" look like all other buttons.
Is there a possibility to trigger that "pressed" animation manually or is there another workaround?
Make sure for those buttons in the Attributes inspector the "Type" is set to "System"

Iphone uibutton click feel

My problem is I have to change UIButton backgroundcolor on click and navigation controller move to next page.
I don't need to use NSTimer to show button effect.
I need to apply this effect for my application all buttons.
I can't able to make my next view controller to wait for show this effect.
If I understand what you're asking correctly, you want all buttons in your application to instantly change background colors upon a button click. You can do this using the UIButton's appearance.
In short, UIAppearance allows developers to easily keep visuals consistent throughout their applications. For more information on what it is and how to use it, click here.
Here's an example:
//Inside of an IBAction that a button clicks
[[UIButton appearance] setBackgroundColor:[UIColor redColor]];

Put different backgrounds in a View Controller

I am doing my first steps creating applications in iOS, so maybe I am asking a dumb question.
In a View Controller, I put some buttons and text fields to make different actions. To improve the organization, I want to put several backgrounds with borders to delimitate the areas where the buttons and text fields are placed.
I tried putting a text label without text and color background, and move my objects over it, but I don't know why the layer is placed over the buttons and text fields. Then, I tried modifying the text label's opacity, but it's totally unsatisfactory.
label1.layer.cornerRadius = 5.0;
label1.layer.masksToBounds = YES;
label1.layer.borderColor = [UIColor lightGrayColor].CGColor;
label1.layer.borderWidth = 1.0;
label1.layer.opacity = 0.1;
label1.layer.backgroundColor = [UIColor whiteColor].CGColor;
Can help me anyone to find a better way to do this or improve my code?
1) you could just use a normal UIView instead of a label with no text (same difference just more correct)
2) in the story board the order in which the UI elements are on the left bar is important, it is basically the draw order of the elements, the ones at the top are drawn first, therefore things below it will be drawn over
You can try in Interface Builder to put multiple UIImageView objects and set your backgrounds as desired from Attributes Inspector and watch out how you put your elements in your UIView. The ones below are drawn after the ones at the top.
If I understood you right, ...
Make yourself familiar with the logic behind the subview hierarchy and the drawing sequence of siblings.
In your interface builder/storyboard you could align the views as followed:
UIView (the default view that is connected to the view controller's view property)
\--UIView (This is a background to group UI Items as buttons or Lables
\-- UILabel
\-- UIButton
\--UIView (the next background)
\-- UILabel
\-- UIButton
When it comes to coorindates then you will notice that the coordinates of each view are relative to its subview. So the UILabel and UIButton might have the same coordinates but each of them will be located within its very subview.
Another option would be:
UIView (the default view that is connected to the view controller's view property)
\--UIView (Background)
\--UIButton
\--UILable
\--UIView (Background)
\--UIButton
\--UILable
Or even
UIView (the default view that is connected to the view controller's view property)
\--UIView (Background)
\--UIView (Background)
\--UIButton
\--UILable
\--UIButton
\--UILable
In that case all UI elemets share the same coordinate space. In that case you need to care for a proper layout.
This one is would not work:
UIView (the default view that is connected to the view controller's view property)
--UIButton
--UILable
--UIButton
--UILable
--UIView (Background)
--UIView (Background)
Even if all coordinates etc. are identical with those of the examples above, the two background views would hide the other UI elements just because they are drawn after the others. And the last one wins the space.

How does UIBarButtonItem know which UIBarStyle the UIToolBar uses?

In Xcode ui builder when one set the UIBarStyle of the UIToolBar (such as BlackTranslucent, for example), the UIToolBarItem matches the background images to it. How does the UIToolBarItem know which style it should use?
I'm trying to do a put a custom colored image on top of the regular background tile (programatically merge a given image on top of the background image). I want to the code to be generalizable enough so that it is able to handle all UIBarStyles. That means I want to know when UIToolBarItem decides which background to use and intercept it so I can compose the button image on the fly.
Without being able to see apple's implementation of UIBarbuttonItem, I posit that they do not, in fact, know what the style of their UIToolbar is. If you look closely, they have the same alpha as their toolbar, and the same overlay (indicative of a subview). Therefore, any image that is below this highlighted layer, added as a subview, should conform to the UIToolbar's style. If you want to use multiple images though (one for each barStyle), you can determine it with self.myToolbar.barStyle and plan appropriately at -viewDidLoad time. As for true image drawing, subclass UIToolbar and override -drawRect: and use [UIImage drawInRect:rect];