How to properly make a UIView work in Interface Builder - ios

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.

Related

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.

Custom Navigation bar Error

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];
}

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];
}

Xcode Scrollview issue when using storyboards and Push

I am fairly new to xcode and having an issue with a scrollview. The scrollview works fine...however when using storyboards and push combined i have a problem. When first viewing the scrollview it works fine however when you push to a new page then go back to the storyboard with the scrollview it no longer scrolls. Im thinking this has something to do with the didload and maybe i should be using willappear? The code is below. I think i need to change this code somehow so that it reloads the storyboard everytime go back to this storyboard... Has anyone else come across this issue or advise on how to fix?
//
// PrintViewController.m
//
//
#import "PrintViewController.h"
#interface PrintViewController ()
#end
#implementation PrintViewController
#synthesize PrintScroller, rememberContentOffset;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[PrintScroller setScrollEnabled:YES];
// Do any additional setup after loading the view.
}
- (void) viewDidAppear:(BOOL)animated {
[PrintScroller setContentSize:CGSizeMake(300, 595)];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
self.PrintScroller.contentOffset = CGPointMake(0, 0);
}
- (void)viewWillDisappear:(BOOL)animated {
self.rememberContentOffset = self.PrintScroller.contentOffset;
[super viewWillDisappear:animated];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.PrintScroller.contentOffset = CGPointMake(0, self.rememberContentOffset.y);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I suspect your problem is here:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.PrintScroller.contentOffset = CGPointMake(0, self.rememberContentOffset.y);
}
This method could be called by the system repeatedly when the user is scrolling. Since you're setting the content offset each time, the view won't move. You should only do this once and then, say, set self.rememberContentOffset to CGPointZero, and then avoid changing the content offset if the remembered offset is zero. Something like:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (self.rememberContentOffset.y > 0) {
self.PrintScroller.contentOffset = CGPointMake(0, self.rememberContentOffset.y);
self.rememberContentOffset = CGPointZero;
}
}

Child View Truncated?

I have a child view controller looking like this:
I am embedding it into another parent view controller,
#implementation ContainmentViewController
- (id)initWithCoder:(NSCoder *)decoder {
if(self = [super initWithCoder:decoder]) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.prototypeVC = [self.storyboard instantiateViewControllerWithIdentifier:#"PrototypeViewController"];
[self addChildViewController:self.prototypeVC];
[self.view addSubview:self.prototypeVC.view];
[self.prototypeVC didMoveToParentViewController:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
This is how it should look like in the original child VC:;
This is how it looks like in the ContainmentVC:
What am I doing wrong?
NOTE: I have to pick "Wants full screen" on the ChildVC otherwise I will see a 20px white space at the top.
I got fed up with playing with storyboard.
So I unchecked Wants Full Screen
and added the following:
self.prototypeVC.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
Everything works now

Resources