How to set UINavigationControl nav bar title? - ios

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.

Related

UISearchBar placeholder text - align to centre programatically

I am using UISearchBar for search purposes using Objective C. I need the placeholder text of search bar to be aligned to the center of the search bar programatically and when user starts typing, text must be aligned left.
Thanks in Advance
I had a same problem like yours before. I couldn't find any workaround and after all I just used UILabel and UISearchBarDelegate methods.
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UISearchBarDelegate>
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController () {
UISearchBar *srchBar;
UILabel *customPlaceHolderForSearchBar;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frm = CGRectMake(0, 0, self.view.frame.size.width, 80);
srchBar = [[UISearchBar alloc] initWithFrame:frm];
srchBar.delegate = self;
[self.view addSubview:srchBar];
customPlaceHolderForSearchBar = [[UILabel alloc] initWithFrame:frm];
customPlaceHolderForSearchBar.text = #"PlaceHolder text";
customPlaceHolderForSearchBar.textColor = [UIColor grayColor];
customPlaceHolderForSearchBar.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:customPlaceHolderForSearchBar];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
customPlaceHolderForSearchBar.hidden = YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
if (searchBar.text.length < 1) {
customPlaceHolderForSearchBar.hidden = NO;
}
}
#end

UISegmentedControl tintcolor can not work after setting backgroundImage

When we do not alter the background image of UISegmentedControl,setting the tintcolor can work perfectly.But once we change the background image, not matter which color we set for the tintcolor,the UIsegmentControl selected color will show up in grey color.How can I solve this problem without customizing a new class?
Use this:
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.segmentedControl setBackgroundImage:[UIImage imageNamed:#"blue.png"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:[UIImage imageNamed:#"green.png"]
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
self.segmentedControl.tintColor = [UIColor orangeColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Output:
GitHub link:
https://github.com/k-sathireddy/SegmentedControlBackgroundWithTint

How can I refer to the view of a subclass from a superclass in Objective-C?

So I have a few different view controllers that I want to have login screens over, which are just a simple text box over a blurred screen. Thus, I thought the best idea would be to make a superclass called Login that all the different view controllers could use. Here's the code for Login.h:
#import <UIKit/UIKit.h>
#interface Login : UIViewController
#property (strong, nonatomic) UIVisualEffectView *blurEffectView;
#property (strong, nonatomic) UITextField *pass;
- (void) enter;
#end
Login.m:
#import "Login.h"
#interface Login () {
NSString *password;
}
#end
#implementation Login
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"In the Login viewDidLoad");
[self presentLogin];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) presentLogin {
NSLog(#"Presenting the login screen");
[self.view setBackgroundColor:[UIColor whiteColor]];
self.pass = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[self.pass setCenter:self.view.center];
[self.view addSubview: self.pass];
[self.pass addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
if (!UIAccessibilityIsReduceTransparencyEnabled()) {
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
self.blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
self.blurEffectView.frame = self.view.bounds;
self.blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view insertSubview:self.blurEffectView belowSubview:self.pass];
}
}
- (void) textFieldDidChange: (UITextField *) t {
if ([self.pass.text isEqualToString:[NSString stringWithFormat:#"17"]]) {
[self.blurEffectView removeFromSuperview];
[self.pass removeFromSuperview];
[self enter];
}
NSLog(#"You're changing the text.");
}
- (void) enter {
//to implement in the subclasses individually
}
#end
The subclass (I am just trying to make one so far) is empty except for a definition of "enter" which simply prints out "login successful". The only output I am getting when I run this is:
In the Login viewDidLoad
Presenting the login screen
Nothing shows up on the screen: just white. I assume this is because I am trying to modify the self.view of the subclass, not the superclass, since the subclass is the thing that is actually getting presented. Is there a better way of doing this? Some other design pattern that I am not thinking of? Or is there an easy way to get around this?
Edit: I just realized that the code I was running was slightly different from what I pasted here. I now updated it, but only the blur shows up, not the text field. Also I realized I had the CGRect wrong, it should be something like CGRectMake(0,0,100,20); so I fixed that, and the text field still doesn't show. Is there a reason that might be happening.
Set your height and width and also set your background color to white . for now it is taking transparent text view
self.pass = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[self.pass setCenter:self.view.center];
[self.pass setBackgroundColor:[UIColor whiteColor]]; // default taking clear color for now

How to make Custom UILabel in ios

In my project there is huge number of UIlabels. I have created one separate custom class for UILabel and I have set all label's properties in that class.
Here my main requirement is some labels' color is white and some labels' text color is black but color is not applying.
CustomUILabel class:-
#import "CustomLabel.h"
#implementation CustomLabel
-(void)layoutSubviews{
[self setupUI];
}
- (void)setupUI {
[self setTextAlignment:NSTextAlignmentLeft];
[self setFont:[UIFont fontWithName:#"Futura" size:14.0]];
[self setBackgroundColor:[UIColor clearColor]];
[self setTextColor:[UIColor blackColor]];
}
#end
Main class:-
#import "MainViewController.h"
#interface MainViewController (){
}
#end
#implementation MainViewController
#synthesize label1,label2;
- (void)viewDidLoad {
[super viewDidLoad];
label1.textColor = [UIColor whiteColor];
label2.textColor = [UIColor blackColor];
}
When you override layoutSubviews, you must call [super layoutSubviews]. But that's not your problem here. The problem is that you're calling setupUI in layoutSubviews, and you shouldn't. Layout happens after viewDidLoad, so your attempt to set the color in viewDidLoad gets overridden later during layout, when setupUI runs.
You should be calling setupUI from the init methods of your subclass:
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupUI];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self setupUI];
}
return self;
}
And you shouldn't override layoutSubviews at all in CustomLabel.
The problem is with your layoutSubview method and your setupUI method.
layoutSubviews is not the proper place to call setupUI. layoutSubviews can be called many times and is most likely being called a couple of times after viewDidLoad is called which is why the colors are being reset back to black.
And always call [super layoutSubviews]; in your layoutSubviews method.
The better place to call your setupUI method is from one or more of the appropriate init... methods and possibly awakeFromNib.

UISegmentedControl tintColor

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

Resources