Custom Navigation bar Error - ios

I'm trying to customise navigation bar in iOS 7. I create custom class that is subclass of UINavigationBar. And there I make the following changes:
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setBarTintColor:[UIColor blackColor]];
[self setTranslucent:YES];
[self setAlpha:0.6f];
}
return self;
}
And there is no any changes, I set custom class in interface builder. What should I do?

Try to override initWithCoder, not initWithFrame, if You are using storyboard to create UINavigationBar.
In situations like that try to set breakpoint to method and check it this method get called at least

If you are using your custom view in IB, you should also override awakeFromNib and do some init there as well. In your code, it could be:
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
-(void)awakeFromNib {
[self setup];
}
-(void)setup {
[self setBarTintColor:[UIColor blackColor]];
[self setTranslucent:YES];
[self setAlpha:0.6f];
}

Related

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.

How to change the background color of a UITableView in the correct way?

I use code to generate UI instead of IB. I've tried setting the backgroundView to nil in the initialization of the table view, and changing the backgroundColor directly in the viewDidLoad, but both failed in changing the background color of the table view.
Finally, I set the backgroundColor in the viewWillAppear, and it works!
So, what happened between viewDidLoad and viewWillAppear on the table view? Why its background color is changed in this phrase?
Here's my code:
// in tableview's init, doesn't work
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
self = [super initWithFrame:frame style:style];
if (self) {
self.backgroundView = nil;
self.backgroundColor = [UIColor grayColor];
}
return self;
}
// loadView
- (void)loadView {
_personalView = [NSBPersonalView new];
self.tableView = _personalView;
self.edgesForExtendedLayout = UIRectEdgeNone;
}
// in view controller's viewDidLoad, doesn't work either
- (void)viewDidLoad {
self.tableView.backgroundColor = [UIColor grayColor];
}
// but in view controller's viewWillAppear, it works
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.tableView.backgroundColor = [UIColor grayColor];
}
I think the problem is that you're not calling super loadView, super viewDidLoad. And you're not assigning self.view in loadView

Issue with a custom view in iOS

How is this possible?
PlayingCardView *view = [[PlayingCardView alloc] initWithFrame:currentFrame];
if ([view isKindOfClass:[PlayingCardView class]])
NSLog(#"checked!");
The if below doesn't work. However, it says that my view is of UIView class, not the PlayingCardView, which is inherited from the UIView. The problem is that because of it I can't send PlayingCardView's messages to my view.
update:
- (instancetype) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
It is possible if you override initWithFrame for PlayingCardView the wrong way. You might be doing something like self = [[UIView alloc] initWithFrame:] instead of self = [super initWithFrame:]. Check any init methods that you have overridden for PlayingCardView.

Add category to UITextView and Override it's init function while UITextView is in xib

I can't intercept the init function that's getting called when it's getting created inside of the xib file.
I want to add borderline to it when it gets created so that I won't need to add it manually.
.h:
#import <UIKit/UIKit.h>
#interface UITextView (FITAddBorderline)
- (id) init;
- (id) initWithFrame:(CGRect)frame;
#end
.m:
#import "UITextView+FITAddBorderline.h"
#implementation UITextView (FITAddBorderline)
- (id) init
{
self = [super init];
if (self) {
[self addBorderline];
}
return self;
}
- (id) initWithFrame:(CGRect)frame {
self = [super init];
if (self) {
self.frame = frame;
[self addBorderline];
}
return self;
}
- (void) addBorderline {
//To make the border look very close to a UITextField
[self.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[self.layer setBorderWidth:2.0];
//The rounded corner part, where you specify your view's corner radius:
self.layer.cornerRadius = 5;
self.clipsToBounds = YES;
}
#end
Views that come from NIBs are initialized with initWithCoder:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self addBorderline];
}
return self;
}
As a side note, I would recommend changing what you are doing and use a subclass instead of a category. You can get yourself into some trouble overriding methods in a category. See more info here.
You just need to implement the awakeFromNib method:
-(void)awakeFromNib
{
[super awakeFromNib];
[self addBorderline];
}

How to properly make a UIView work in Interface Builder

I have a custom view that I would like to use in Interface Builder. It's setup like the following:
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initialize];
}
return self;
}
-(void)initialize {
....
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initialize];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
...frame stuff
}
but when I use the class in interface builder it is not receiving the frame, just it's bounds. Meaning the origin stays at 0,0 but the width/height do get set correctly. If anyone can point out whats wrong I would be very greatful.
UPDATE:
Just spotted this in IB:
Override - (void)awakeFromNib; for your custom view. At that point, your frame should be setup.

Resources