Sublayer only draws when created in initWithFrame, not initWithCoder - ios

I have a custom view with a sublayer (CAShapeLayer) and subview (UILabel). When I create the layer in initWithCoder and set the background color, it always shows up as black. However, if I move the code into initWithFrame, the color shows up successfully.
Are we not supposed to create sublayers in initWithCoder?
Here is the only way I can get my code to work:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.colorLayer = [CAShapeLayer layer];
self.colorLayer.opacity = 1.0;
[self.layer addSublayer:self.colorLayer];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.textLabel = [[UILabel alloc] initWithFrame:self.bounds];
self.textLabel.font = [UIFont primaryBoldFontWithSize:12];
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.textAlignment = NSTextAlignmentCenter;
self.textLabel.backgroundColor = [UIColor clearColor];
[self addSubview:self.textLabel];
}
return self;
}
- (void)drawRect:(CGRect)rect {
//Custom drawing of sublayer
}
UPDATE:
Turns out in my drawRect I was setting the fill color wrong. I should have used colorLayer.fillColor = myColor.CGColor instead of [myColor setFill] then [path fill]

The difference between initWithFrame: and initWithCoder: is that initWithCoder: is called when the view is created from storyboard/nib.
If you add it programatically, for example:
UIView *v = [[UIView alloc] initWithFrame:...];
[self.view addSubview:v];
initWithFrame: is called.
The good idea is to create base init method and call it in both init. In that way the initialisation sets up all of the properties in both scenario, when the view is added programatically or in storyboard.
For example:
-(void)baseInit {
self.colorLayer = [CAShapeLayer layer];
self.colorLayer.opacity = 1.0;
//... other initialisation
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self baseInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self baseInit];
}
return self;
}

Related

UIView strange behaviour with BOOL

I've made my custom UIView that I'm representing on UIViewController. View is presented on screen, but BOOL that I've setted on that instance of UIView to YES is not recognized.
Code:
UIView implementation:
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self createInterface];
}
return self;
}
-(void)createInterface
{
if (self.isSplash == YES) {
self.backgroundColor = [UIColor clearColor];
}
else {
self.backgroundColor = DARK_BLUE;
[self setupTimer];
}
....
ViewController:
self.sponsorView = [[SponsorMechanismView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.logo.frame)+10, SCREEN_WIDTH, 100)];
self.sponsorView.isSplash = YES;
[self.view addSubview:self.sponsorView];
So, I set BOOL to YES, but in this UIView it is always NO.
As Fabio said, createInterface has already been executed.
What you can do is create your own init function. Something like this :
- (id)initWithFrame:(CGRect)frame isSplash(BOOL)isSplash
{
self = [super initWithFrame:frame];
self.isSplash = isSplash;
if (self) {
[self createInterface];
}
return self;
}
And the call of the function will be like :
self.sponsorView = [[SponsorMechanismView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.logo.frame)+10, SCREEN_WIDTH, 100) isSplash:YES];
- (id)initWithFrame:(CGRect)frame AndIsFlash:(BOOL)isFlash{
self = [super initWithFrame:frame];
if (self) {
[self createInterfaceAndisFlash:isFlash];
}
return self;
}
-(void)createInterfaceAndisFlash:(BOOL)isFlash
{
if (isFlash == YES) {
self.backgroundColor = [UIColor clearColor];
}
else {
self.backgroundColor = [UIColor blueColor];
}
}
Hope you Got an Idea.
As mentioned in the comments:
The creatInterface method has already been executed when you set isSplash.
You can solve this by overwriting the setter of isSplash and calling createInterface there.
Code example here:
- (void)setIsSplash:(BOOL)isSplash
{
if (isSplash != _isSplash)
{
_isSplash = isSplash;
[self createInterface];
}
}

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

Views disappearing from UITableViewCell when selected

I have a UITableView with custom UITableViewCells. The content of the cell is kept in a separate class extending UIView (i have 3 kind of contents which i switch by hiding and showing views, i took this approach because of the way i wanted to make the transition between cells).
So let's assume i have one of the views visible and added to the contentView of the cell. This view contains some text and some rectangles made of UIViews. Everything good till now, but the weird thing is when i touch the cell, the rectangles simply disappears, the text stays the same. Do you know what might be the problem? I tried to add the view to the backgroundView as well, it's doing the same.
- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.active = NO;
state = -1;
// Touch is not working if the view has no bounds
self.backgroundView = [[UIView alloc] init];
self.backgroundColor = [UIColor clearColor];
self.selectedBackgroundView = [[UIView alloc] init];
self.selectedBackgroundView.backgroundColor = [UIColor clearColor];
self.clipsToBounds = YES;
self.contentView.clipsToBounds = YES;
// Add empty view
emptyView = [[View1 alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) andRadius:CELL_DOT_RADIUS];
emptyView.userInteractionEnabled = NO;
[self.backgroundView addSubview:emptyView];
The view:
- (id) initWithFrame:(CGRect)frame andRadius:(int)r {
self = [super initWithFrame:frame]; radius = r;
if (self) {
int outerlineWeight = 1;
timelineView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width/4, frame.size.height/2, 1, 1)];
[self addSubview:timelineView];
dotView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width/4-radius, frame.size.height/2-radius, radius*2, radius*2)];
dotView.layer.cornerRadius = radius;
dotView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self addSubview:dotView];
UIView *n = [[UIView alloc] initWithFrame:CGRectMake(160, 0, 50, 20)];
n.backgroundColor = [UIColor redColor];
[self addSubview:n];
middleText = [[UILabel alloc] initWithFrame:dotView.frame];
middleText.font = [UIFont systemFontOfSize:12];
middleText.textColor = [UIColor grayColor];
middleText.backgroundColor = [UIColor clearColor];
middleText.textAlignment = NSTextAlignmentCenter;
[self addSubview:middleText];
This are the sources so you can try for yourself if you want. As you can see the orange rectangle disappears but the text not. For what i need to do nothing should disappear, in the best case i want to change their colours. http://ge.tt/6wRBObD1/v/0?c
You are adding it to background view. When selected the selected background view will display not background view. In your custom cell try
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected) {
[self.backgroundView addSubview:emptyView];
}
else{
[self.selectedBackgroundView addSubview:emptyView];
}
// Configure the view for the selected state
}
Or add empty view to contentview
one thing for frame settings u need to override "layoutsubviews" method ...
in custom cell
init all the views in initialisation method i,e ur - (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier method and add it to cell
in - (void)layoutSubviews just set the frame for ur views in the cell,
use tag property to access the views in the layoutsubviews, if it is not the property
- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.active = NO;
state = -1;
// Touch is not working if the view has no bounds
self.backgroundView = [[UIView alloc] init];
self.backgroundColor = [UIColor clearColor];
self.selectedBackgroundView = [[UIView alloc] init];
self.selectedBackgroundView.backgroundColor = [UIColor clearColor];
self.clipsToBounds = YES;
self.contentView.clipsToBounds = YES;
// Add empty view
//emptyView = [[View1 alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) andRadius:CELL_DOT_RADIUS];//dont set the frame heare do it layoutsubviews
emptyView = [[View1 alloc] init];
emptyView.userInteractionEnabled = NO;
emptyView.tag = 100;
[self.backgroundView addSubview:emptyView];
- (void)layoutSubviews
{
[super layoutSubviews];
UIView *emptyView = [self viewWithTag:100];//your background view heare u can get the frame values for ur cell
emptyView.frame = self.bounds;//better do it for other views in cell also
//test it by setting some background color
}
Hope this helps u ...:)
Ok, so like i've said in my last comment the backgroundColor of any subview is the problem, in the selected state is cleared. For cells created in interface builder the problem does not occurs, my cells are created programatically.
I fixed this by drawing with CoreGraphics in drawRect method.
As with a UICollectionView a UITableView has a background view that is build before AutoLayout. If you use autoLayout you should make sure the backgroundView has a frame. Otherwise is will disappear.
In your cell subclass you can do:
-(void)setBackgroundView:(UIView *)backgroundView
{
super.backgroundView = backgroundView;
self.backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
[self.backgroundView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
}
In swift you need to declare viewcontroller object globally that would result in Strong, in case if you declare locally it results in keep disappearing the cells.
var refineViewController : RefineViewController?
then you can access that controller using below code that would not result in disappearing cells.
func showRefineView(isFindHomeTab isFindHomeTab : Bool){
refineViewController = RefineViewController(nibName: String(BaseGroupedTableVC),bundle : nil)
refineViewController!.refineSearchDelegate = self
refineViewController!.view.frame = CGRectMake(0, -490, self.view.frame.size.width, self.view.frame.size.height)
UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveEaseOut, animations:
{
self.refineViewController!.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
self.refineViewController!.isFindHomeTab = isFindHomeTab
}, completion: nil)
self.view.addSubview(refineViewController!.view)
}

Adding a CALayer sublayer inside of UIView init

I'm trying to add a CALayer as a sublayer in a UIView subclass, but when I add the sublayer inside the init method I get EXC_BAD_ACCESS when I add the view to another view or window.
Init method:
- (id)initWithTitle:(NSString *)title message:(NSString *)message
{
if ((self = [super init]))
{
self.title = title;
self.message = message;
self.alertLayer = [[CALayer alloc] init];
self.layer.cornerRadius = kCORNER_RADIUS;
self.layer.shadowRadius = 3.0;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(15, 20);
self.layer.shadowOpacity = 1.0;
self.alertLayer.delegate = self;
self.alertLayer.masksToBounds = YES;
self.alertLayer.cornerRadius = kCORNER_RADIUS;
[self.layer addSublayer:self.alertLayer]; // This line of code seems to cause EXC_BAD_ACCESS
}
return self;
}
EXC_BAD_ACCESS is caused after calling [self.view addSubview:alertView] inside a view controller or UIWindow.
You have two layers (self.layer and self.alertLayer) which have the same delegate self, this leads to an infinite recursion in internal method -[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:] when added this view (self) to the view tree. Therefore you must remove self.alertLayer.delegate = self; to avoid crash. If you need to delegate for alarmLayer you can create distinct object.

CALayer, subLayer alpha overriding

I have a CALayer ( superlayer ) with 0.3 opacity property. The superlayer contains another CALayer(sublayer). Although sublayer does not have a opacity property superLayer's opacity affects sublayer's appearance. Is there a method that I can override superLayer'a opacity property.
SuperLayer
#implementation SuperView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setOpaque:NO];
[self.layer setOpaque:NO];
}
return self;
}
-(void)drawRect:(CGRect)rect
{
CALayer* superLaer=self.layer;
superLaer.backgroundColor = [UIColor blackColor].CGColor;
superLaer.shadowOffset = CGSizeMake(0, 3);
superLaer.shadowRadius = 5.0;
superLaer.shadowColor = [UIColor blackColor].CGColor;
superLaer.shadowOpacity = 0.6;
superLaer.frame = CGRectMake(10, 10, 300,300);
superLaer.borderColor = [UIColor blackColor].CGColor;
superLaer.borderWidth = 2.0;
superLaer.cornerRadius = 10.0;
superLaer.cornerRadius = 10.0;
superLaer.masksToBounds=YES;
superLaer.opacity = 0.1;
}
#end
SubLayer
#implementation SubView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setOpaque:NO];
[self.layer setOpaque:NO];
}
return self;
}
-(void)drawRect:(CGRect)rect
{
CALayer* superLaer=self.layer;
superLaer.backgroundColor = [UIColor redColor].CGColor;
superLaer.shadowOffset = CGSizeMake(0, 3);
superLaer.shadowRadius = 5.0;
superLaer.shadowColor = [UIColor blackColor].CGColor;
superLaer.shadowOpacity = 0.6;
superLaer.frame = CGRectMake(100, 100, 100,100);
superLaer.borderColor = [UIColor blackColor].CGColor;
superLaer.borderWidth = 2.0;
superLaer.cornerRadius = 10.0;
superLaer.cornerRadius = 10.0;
superLaer.masksToBounds=YES;
}
#end
I have added the subView to the superView in Viewcontroller.
Based on your question, The super layer's opacity controls the sublayers' opacity,
you could subclass CALayer and override some of its properties in init method like
#implementation SomeSuperCALayer
-(id)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
#define W_H 100.0
#define X_Y 50.0f
-(void)setup
{
self.frame = CGRectMake(X_Y, X_Y, W_H, W_H);
self.backgroundColor = [UIColor blueColor].CGColor;
self.opacity = 1.0f; // big opacity
}
#implementation SomeSubCALayer
-(id)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
#define W_H 100.0
#define X_Y 150.0f
-(void)setup
{
self.frame = CGRectMake(X_Y, X_Y, W_H, W_H);
self.backgroundColor = [UIColor blueColor].CGColor;
self.opacity = .5f; // small opacity
}
# implementation MyViewController
//
-(void)viewDidLoad
{
SomeSuperCALayer *superLayer = [[SomeSuperCALayer alloc] init]; SomeSubCALayer *subLayer = [[SomeSubCALayer alloc] init];
//add sub layer to super layer
[superLayer addSubLayer:subLayer];
// add super layer to main View
[self.view.layer addSubLayer:superLayer];
//
//...
}
Also don't use drawRect for calling layer's properties, drawRect is mean't for drawing like the Apple's documentation dictates:
/ Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

Resources