UISegmentedControl tintColor - ios

I'm having problems getting UISegmentedControl to show the desired tint color.
// AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// need red tint color in other views of the app
[[UIView appearance] setTintColor:[UIColor redColor]];
return YES;
}
// ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *items = #[#"Item 1", #"Item 2"];
UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:items];
// would like to have this control to have a green tint color
control.tintColor = [UIColor greenColor];
[self.view addSubview:control];
}
How to make UISegmentedControl use the green tint color?

I ended up creating a category for the desired behaviour. Subview structure looks like this:
UISegment
UISegmentLabel
UIImageView
UISegment
UISegmentLabel
UIImageView
So two loops are required for the desired effect (otherwise some parts stay in old tint color).
UISegmentedControl+TintColor.h
#import <UIKit/UIKit.h>
#interface UISegmentedControl (TintColor)
#end
UISegmentedControl+TintColor.m
#import "UISegmentedControl+TintColor.h"
#implementation UISegmentedControl (TintColor)
- (void)setTintColor:(UIColor *)tintColor {
[super setTintColor:tintColor];
for (UIView *subview in self.subviews) {
subview.tintColor = tintColor;
for (UIView *subsubview in subview.subviews) {
subsubview.tintColor = tintColor;
}
}
}
#end

Try something like this ?
for (UIView *subView in mySegmentedControl.subviews)
{
[subView setTintColor: [UIColor greenColor]];
}
But it actually appears that it is a known issue in iOS 7, I don't know if it has been fixed in iOS 8.
"You cannot customize the segmented control’s style on iOS 7. Segmented controls only have one style"
UIKit User Interface Catalog

Related

Button change position when alert view is showed

I am showing an alert on my UITableView.
This is the code
-(void)showAlertMaintenance{
AppDelegate * appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[RMUniversalAlert showAlertInViewController:self withTitle:nil message:appDelegate.maintenanceStr cancelButtonTitle:#"OK" destructiveButtonTitle:nil otherButtonTitles:nil tapBlock:^(RMUniversalAlert * _Nonnull alert, NSInteger buttonIndex) {
if(buttonIndex == alert.cancelButtonIndex){
}
}];
}
The button is a subviews of one section header in my UITableview
UIButton *sendInviteCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sendInviteCodeButton addTarget:self
action:#selector(shareInvitationCode)
forControlEvents:UIControlEventTouchUpInside];
[sendInviteCodeButton setTitle:#"招待コードをシェアする" forState:UIControlStateNormal];
[sendInviteCodeButton sizeToFit];
[sendInviteCodeButton setTitleColor:[UIColor colorWithRed:215.0f/255.0f green:116.0f/255.0f blue:52.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
sendInviteCodeButton.translatesAutoresizingMaskIntoConstraints = NO;
sendInviteCodeButton.frame = CGRectMake(20, 155.0f, SCREEN_BOUNDS_SIZE_PORTRAIT_WIDTH - 40, 30.0f);
[sendInviteCodeButton.layer setMasksToBounds:YES];
[sendInviteCodeButton.layer setCornerRadius:5.0f];
[sendInviteCodeButton.layer setBackgroundColor:[UIColor whiteColor].CGColor];
[sendInviteCodeButton setBackgroundColor:[UIColor whiteColor]];
[sendInviteCodeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
[invitationView insertSubview:sendInviteCodeButton atIndex:0];
Before
After
When the alert is showed, the button is being pushed to top left corner of the screen.
Please help!
If you already calculate specific frame for your button, you should remove [sendInviteCodeButton sizeToFit];.
Just remove [sendInviteCodeButton sizeToFit];
Hope it help. :)
Since you are relying on frames to setup the layout you should have a dedicated UIView subclass for your tableview header.You need to override layoutSubviews method because at this point the header view will have it's own frame correctly set so your relative calculations for subviews (including the button) will make sense. Here's a quick sample code:
#import <UIKit/UIKit.h>
#interface MyTableViewHeader : UIView
#end
#implementation MyTableViewHeader
-(instancetype)init
{
self = [super init];
if (self) {
//setup button here (omit setting frames)
}
return self;
}
-(void)layoutSubviews
{
//set the button frame here
[super layoutSubviews];
}
#end
#interface ViewController : UIViewController<UITableViewDelegate>
#property (nonatomic, strong) MyTableViewHeader *header;
#end
#implementation ViewController
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//You will probably want some logic here
//to decide if you want to display the header
if (!self.header) {
self.header = [MyTableViewHeader new];
}
return self.header;
}
#end
As an alternative you could also setup autolayout for your table header view.
I found out the problem caused by this
[sendInviteCodeButton.layer setMasksToBounds:YES];
setMasksToBounds - is a Boolean indicating whether sublayers are clipped to the layer’s bounds

How to set UINavigationControl nav bar title?

I've tried almost everything on stack overflow, but to no avail. I've tried it all, but still nothing, so I'm here. I'm simply just trying to set the title to a white color and then set the title to "Terms of Service". Here's what I have:
#import <UIKit/UIKit.h>
#interface TOCViewController : UINavigationController
#end
//
#import "TOCViewController.h"
#interface TOCViewController ()
#end
#implementation TOCViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
self.navigationBar.barTintColor = [UIColor colorWithRed:52.0/255.0 green:170.0/255.0 blue:220.0/255.0 alpha:1.0];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
To set title in UINavigationBar set UIViewController title
- (void)viewDidLoad {
[super viewDidLoad];
[self setTitle:#"Terms of Service"];
}
For title color you need to set text attributes of UINavigationBar.
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName : [UIColor whiteColor]};
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
}
The title that appears in a navigation controller's navigation bar is not the title of itself. It is the title of its current child view controller. Thus the reason you see nothing happening is that your code is in the wrong view controller. You have subclassed UINavigationController, a thing there is rarely if ever any reason to do these days.

Change tint color of UIAlertview and UIActionsheet buttons

I am trying to adapt my application for iOS 7. The issue I am having is I can not change the tint color of some controls.
I did add
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
if (IOS7_OR_LATER)
self.window.tintColor = [self greenTintColor];
to my app delegate's
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
It mostly helped but color of message box and action sheet buttons is still the default blue.
How can I recolor all such buttons too?
Some screenshots:
As UIAlertView is deprecated You can. Use UIAlertController.
You can use tintColor property.
OLD
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
-From Apple Doc
You can use tintColor property or You can use Some Custom Library for that, you can find it at cocoacontrols.com.
I was able to change the cancel button's text color to white in app delegate.
[[UIView appearance] setTintColor:[UIColor whiteColor]];
For Actionsheet You can use
Utilize the willPresentActionSheet delegate method of UIActionSheet to change the action sheet button color.
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
button.titleLabel.textColor = [UIColor greenColor];
}
}
}
Combining best answers above, and updated for deprecation:
[[UIView appearanceWhenContainedInInstancesOfClasses:#[[UIAlertController class]]] setTintColor:[UIColor greenColor]];
or Swift:
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .green
Works in 2018, Swift 4 / iOS 12.
You can adjust the color by searching and modifying the UILabel in the subview hierarchy of the alert window that is created right after showing the alert:
- (void)setButtonColor:(UIColor*)buttonColor {
dispatch_after(dispatch_time(0,1), dispatch_get_main_queue(), ^{
NSMutableArray *buttonTitles = [NSMutableArray array];
for (NSUInteger index = 0; index < self.numberOfButtons; index++) {
[buttonTitles addObject:[self buttonTitleAtIndex:index]];
}
for (UILabel *label in [[[UIApplication sharedApplication] keyWindow] recursiveSubviewsOfKind:UILabel.class]) {
if ([buttonTitles containsObject:label.text]) {
label.textColor = buttonColor;
label.highlightedTextColor = buttonColor;
}
}
});
}
[alert show];
[alert setButtonColor:UIColor.redColor];
The recursiveSubviewsOfKind: method is a category on UIView that returns an array of views in the complete subview hierarchy of the given class or subclass.
for UIAlertView with colored buttons you can use the cocoapod "SDCAlertView"
about CocoaPods: http://www.cocoapods.org
how to install CocoaPods: https://www.youtube.com/watch?v=9_FbAlq2g9o&index=20&list=LLSyp50_buFrhXC0bqL3nfiw
In iOS 6.0 create custom view in App delegate
.h
UIView* _loadingView;
UIView* _subView;
UIActivityIndicatorView*loadingIndicator;
UITabBarController *tabBar_Controller;
NSTimer *timer;
#property (strong, nonatomic) UIView* _loadingView;
#property (strong, nonatomic) UIView* _subView;
.m- (void)fadeScreen
{
[UIView beginAnimations:nil context:nil]; // begins animation block
[UIView setAnimationDuration:3.0]; // sets animation duration
[UIView setAnimationDelegate:self]; // sets delegate for this block
[UIView setAnimationDidStopSelector:#selector(finishedFading)];
self.txtview.alpha = 0.0; // Fades the alpha channel of this view
[UIView commitAnimations]; // commits the animation block.  This
}
- (void) finishedFading
{
[self.txtview removeFromSuperview];
}
- (void)showConnectivity:(NSString *)strTitle
{
[_loadingView setBackgroundColor:[UIColor clearColor]];
[_loadingView setAlpha:0.5];
[_loadingView.layer setCornerRadius:10];
[self.window addSubview:_loadingView];
[_loadingView setHidden:NO];
[_subView.layer setCornerRadius:7];
[_subView setBackgroundColor:[UIColor colorWithHue:0.0f saturation:0.0f brightness:0.0f alpha:0.6]];
[_subView setOpaque:YES];
[self.window addSubview:_subView];
[_subView setHidden:NO];
[_loadingView setHidden:NO];
[_subView setHidden:NO];
loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[loadingIndicator setFrame:CGRectMake(85,10,35,35)];
[_subView addSubview:loadingIndicator];
[loadingIndicator setBackgroundColor:[UIColor redColor]];
[loadingIndicator startAnimating];
UILabel *_lab=[[UILabel alloc]initWithFrame:CGRectMake(8,10,72,45)];
[_lab setText:strTitle];
[_lab setTextColor:[UIColor whiteColor]];
[_lab setBackgroundColor:[UIColor clearColor]];
[_lab setFont:[UIFont boldSystemFontOfSize:13.0]];
[_lab setTextAlignment:NSTextAlignmentCenter];
[_subView addSubview:_lab];
}
- (void)CoonectingViewHidden
{
[_loadingView setHidden:YES];
[_subView setHidden:YES];
NSArray *_aryViews = [_subView subviews];
for(int i = 0; i<[_aryViews count];i++)
{
id obj = [_aryViews objectAtIndex:i];
if(![obj isKindOfClass:[UIActivityIndicatorView class]])
[obj removeFromSuperview];
}
[loadingIndicator stopAnimating];
[loadingIndicator hidesWhenStopped];
}
in using .m
#import"Appdelegate.h"
- (void)showLoadingIndicator:(NSString *)message
{
AppDelegate *delegateObj2=(AppDelegate *)[UIApplication sharedApplication].delegate;
[delegateObj2 showConnectivity:message];
}
-(void)stopLoading
{
AppDelegate *delegateObj3=(AppDelegate *)[UIApplication sharedApplication].delegate;
[delegateObj3 CoonectingViewHidden];
}
// [self showLoadingIndicator:#"Loading"];
n
[self stopLoading];

The background colour of a grouped UITableView will not go transparent in iOS7

I am working on an app, which worked perfectly fine with iOS 6. With iOS7 the app has couple of layout and general few appearance issues. One is not being able to set the background color of a grouped tableview to transparent. Heres is my code which does not work anymore
tableForDetails = [[UITableView alloc]initWithFrame:CGRectMake(0, yAxisTable, 320, 150) style:UITableViewStyleGrouped];
UIColor *backGroundColor = [UIColor clearColor];
UIView *bview = [[UIView alloc]init];
[bview setBackgroundColor:backGroundColor];
[tableForDetails setBackgroundView:bview];
Help is greatly appreciated. Thanks
To set background transparency for a grouped uiTableview in iOS7 is really quite easy. The solution is even more simple than I originally thought.
[tableForDetails setBackgroundColor:[UIColor clearColor]];
or for only semi transparent
[tableForDetails setBackgroundColor:[[UIColor alloc]initWithRed:1.0 green:1.0 blue:1.0 alpha:0.5]];
This worked fine for me.
I fixed it with this:
tableView.backgroundView = UIView.new;
I put any kind of improvements on Apple components in a super class. The following upgrades are applied in BasicTableView.m, which extends UITableView:
1) overrides setBackgroundColor to work with iOS 7 and above, while maintaining backward compatibility with iOS 6 and 5.
2) overrides initWithCoder: and initWithFrame:style: to initialize the background color to a default transparent background
- (id) initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self) {
[self setup];
}
return self;
}
- (id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
self = [super initWithFrame:frame style:style];
if (self) {
[self setup];
}
return self;
}
- (void) setup
{
//Set the BG color to clear color by default
[self setBackgroundColor:[UIColor clearColor]];
}
- (void) setBackgroundColor:(UIColor *)backgroundColor
{
if (self.style == UITableViewStyleGrouped) {
if (majorSystemVersion() < 7) {
UIView *tableBgView = [[UIView alloc] init];
tableBgView.backgroundColor = backgroundColor;
self.backgroundView = tableBgView;
} else {
[super setBackgroundColor:backgroundColor];
}
}
}
//majorSystemVersion() is defined elsewhere in a utility file
int majorSystemVersion(void) {
NSString *systemVersion = [UIDevice currentDevice].systemVersion;
NSArray *systemVersionComps = [systemVersion componentsSeparatedByString:#"."];
NSString *majorVersion = [systemVersionComps objectAtIndex:0];
return [majorVersion intValue];
}
UITableViewCell is in white Background by default in iOS 7
put this inside your cellforRowAtIndexPath
[cell setBackgroundColor:[UIColor clearColor]]

How can remove the bottom shadow of UINavigationBar?

I am using custom navigationBar within my projects, but it's giving bottom drop shadow of UINavigation bar, How we can remove it, Please provide the answer for it if any work on it.
TIA :)
The easiest way to remove the shadow underneath the UINavigationBar is to set a custom background image and then set the shadow image to a blank UIImage.
CustomViewController.m
- (void)viewDidLoad
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"Background"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
In the above example, "Background" would be a PNG image in your project.
in iOS7:
for (UIView *view in self.navigationController.navigationBar.subviews) {
for (UIView *view2 in view.subviews) {
if ([view2 isKindOfClass:[UIImageView class]]) {
if (view2.frame.size.height < 1) {
[view2 removeFromSuperview];
}
}
}
}

Resources