Styling Buttons in IOS Xcode - ios

I am new to IOS development. In windows phone development we use to define a style(appearance e.g) of the button control in app Global place and just use that style anywhere on any button in the app by assigning the style property.
Is there anything similar in IOS XCode? I know I can customize the button in the property and appearance windows in XCode. But in this way I have to give style to each and every similar button in the app. and if there is any change I have to change it everywhere. What IOS developers do in these kind situations?

If you want to style all buttons in your app in a standard way, then you would style the appearance proxy for UIButton. You get one by calling the class method UIButton.appearance(), and then styling the returned value the way you want your buttons to look. You can also do a more fine-grained appearance by calling methods like UIButton.appearanceWhenContainedIn().

Create a UIButton subclass.
For example for creating a UIButton with rounded corners and a border:
Header file
#import <Foundation/Foundation.h>
#interface XYRoundedCornerButton : UIButton
#end
Implementation file
...import headers etc...
#implementation XYRoundedCornerButton
- (void)awakeFromNib {
[super awakeFromNib];
self.layer.cornerRadius = 8.0f;
self.layer.masksToBounds = YES;
self.layer.borderRadius = 1.0f;
self.layer.borderColor = UIColor.redColor;
}
#end

If you are using storyboard, You don't need to add any code in class.
Just drag one button and set all the properties you want, whether background colour, corner radius etc, whatever you want. And then either copy it and paste wherever you want or select the button, ALT+DRAG it .

Related

How to use UITextField UIAppearance based on user Interaction enabled trait

I am trying to find a way to have UITextField appearance is distinguishable when user interaction is disabled. I have to do this application wide. Thought using UIAppearance could be an easy way to do. But couldn't figure out how to use that with user interaction enabled trait of text field.
Can you help? Or Is there an alternate way to do the same thing centrally.
Note: Text field user interaction enabled state on a given instance can get toggled based on user action.
Thanks
I don't know of an app wide way to do it if you're using text fields directly, but if you were to subclass UITextField and override setEnabled:, you could make a change to the background there.
#implementation MYTextField
- (void)setEnabled:(BOOL)enabled
{
[super setEnabled:enabled];
self.backgroundColor = [UIColor lightGrayColor];
}
#end
There is already a faint grey background for UITextFields which are disabled, but if this is not enough you can do more as above. Alternatively, you could use setDisabledBackground: on each UITextField to set an image for display when disabled - I accept that this isn't a great solution with the current design paradigms.

Label over button, how to highlight text on button press in iOS7?

I have a button and multiple labels placed over the button in storyboard. For the button I specified a default and a highlight state. Also for the labels I specified the highlight color in storyboard.
However on button press the font color of the label does not change to white. Am I missing something? I would like to configure this behavior in storyboard, not programmatically. Is this possible? Or do I have to create a custom button?
The problem is that the UILabel does not get the touch events because it simply does not handle touch events by design, it's just for showing text.
You might want to create a subclass of a UIButton but this is not a good idea since it's kind of a cluster class.
Best way to do it is creating a custom button class by subclassing either UIControl or UIView. With the later you could add it in your storyboard by changing their class to one of your button subclasses. In the subclass make your customizations in the initWithCoder: method.
If you decided to choose the UIControl way of doing this. Look at setHighlighted: method:
- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted: highlighted];
// Highlight your labels here
}
Useful link: UIControl Class Reference

Round Rect UIBarButton in iOS 7

I Am trying to create a UIBarButton as a flat Round Rect. sort of a mix between iOS 7 and 6.
Heres an example from the Facebook app
I want a Button that looks like the "send" button.
Anyone has any idea?
Im actually surprised that Apple don't give this option for us while they use it in quite a few places. sometimes just writing in colour is not enough to make the user understand its a button.
If you were to put a UIButton inside a UIBarButtonItem and set the backgroundColor, then you could use the cornerRadius property of it's layer:
self.button.layer.cornerRadius = 5;
In order for this to work, you need the CoreGraphics.framework and #import <QuartzCore/QuartzCore.h>
You can just provide a custom button background image with rounded corners and set it as the background image inside the storyboard.

How do I make UITextview like Facebook comment view?

I want to make UITextview like in the Facebook comment view. How can I do it without using any external libraries?
Here is example image:
First, set the appearance of your UITextField to Alert to get the black keyboard.
Then, you only need two more views :
a black one for the background
a white one which will be the background of your text field, which you programmatically apply the following code to (don't forget to #import <QuartzCore/QuartzCore.h> first):
view.layer.cornerRadius = 10;
i have get sample code for this - here it is YIPopupTextView
What they do is use an image, that's it! They also probably expand the size of the UITextfield to make it fit the entire screen but yeah it's pretty easy. If you go into Interface Builder you can see a thing that says image. If you want to do this programmatically there is a value is textfield.background (A UIImage).
If you don't want to create an image you can create a custom UITextField subclass and use the -(void) drawInRect:(CGRect)rect function, and draw it with Quartz, and in interface builder set the UITextField class to your textfield.

How to change border color of UIPopoverController? [duplicate]

I'm working on an iPad application and I'm using UIPopoverControllers. I'm at the part where the app needs to be branded and styled and i'm wondering how to change the color / tint of the UIPopoverController? Standard is dark blue but it needs to be another color..
is this possible?
Greets, Thomas
This is possible starting in iOS 5.0 by subclassing the abstract class UIPopoverBackgroundView and assigning your subclass to the popoverBackgroundViewClass property on your UIPopoverController instance. Unfortunately there is no tintColor property as the popover needs to use images for it's arrow and border in order to achieve smooth animations during dynamic resizing. You can learn more about how to customize the appearance of a UIPopoverController in the UIPopoverBackgroundView Class Reference
It's impossible for now.
It's what I call the "Box in a Box" model. You get control of the box inside of the box (the UIViewController inside of the UIPopoverController), but you have very limited control over the actual popover itself. Outside of the arrow direction and the size, you can't change much else. There are also options for a modal effect popover, which dims everything else when it shows up, but I haven't tried to get it working.
I'm sure you've noticed there is no UIPopover class by now.
The answer you want to hear:
If you really want to style one that bad, just write your own. It's really not that hard.
The link you want to click:
Cocoacontrols is an index of iOS and OSX components available on GitHub, they have some popover stuff.
iOS 7 introduces backgroundColor property of UIPopoverController which affects/includes the navigation background color as well as arrows of popover.
#property (nonatomic, copy) UIColor *backgroundColor NS_AVAILABLE_IOS(7_0);
Usage example:
if ([self.popoverVC respondsToSelector:#selector(setBackgroundColor:)]) { // Check to avoid app crash prior to iOS 7
self.popoverVC.backgroundColor = [UIColor greenColor]; // [UIColor colorWithPatternImage:#"..."] doesn't reflect the color on simulator but on device it works!
}
Note - As of now (iOS 7.0.3), in some cases (like set color using colorWithPatternImage:), the simulator (and even some devices) doesn't honor the color.
Throwing my hat in here;
I've leveraged UIPopoverBackgroundViews in iOS 5+ to add a simple tintColor property onto UIPopoverControllers.
PCPopoverController: https://github.com/pcperini/PCPopoverController
I try to trick it by customizing the view controller inside the popover and then hiding the popover border using this code:
UIView * border = [[insideViewController.view.superview.superview.superview subviews] objectAtIndex:0];
border.hidden = YES;
The app is actually still in development so I'm hoping other people will comment on this solution.
check out these latest projects leveraging UIPopoverBackgroundView
https://github.com/CRedit360/C360PopoverBackgroundView
https://github.com/GiK/GIKPopoverBackgroundView
from ios 5 onward it is can be done, here is a library
https://github.com/ddebin/DDPopoverBackgroundView
just look at the documentation , and it is quite easy
good luck
You can use Elegant Popover cocoapod for just that. You can customise shape and colour of the arrow and the popover itself. Also, you can add colourful borders to the popover.
I know this is a lousy constructed answer, but I've just been playing with the UIPopoverController's views. They do exist.
The only way to access them is from your view that is sitting in the UIPopovercontroller.
I have a navigation controller so I follow this hierarchy
UIView *test = ((UIView *)[[[self.navigationController.view.superview.superview.subviews objectAtIndex:0] subviews] objectAtIndex:1]);
UIView *test2 = ((UIView *)[[[self.navigationController.view.superview.superview.subviews objectAtIndex:0] subviews] objectAtIndex:1]);
test.backgroundColor = [UIColor greenColor];
test2.backgroundColor = [UIColor greenColor];
This isn't exactly the end goal, but it is really close.
you'll find that the_view_in_the_popover.superview.superview (maybe just one superview if you are not reaching out from a navigation controller view) is a UIPopoverView. If you cast it as a UIView and treat it as a UIView you're not really breaking any rules. I guess that is really up to apple though.
Remove UIPopoverController border:
NSArray* subviews = ((UIView*)[popupController.contentViewController.view.superview.superview.superview.subviews objectAtIndex:0]).subviews;
for(UIView *subview in subviews){
[subview removeFromSuperview];
}

Resources