I'm trying to do some CALayer stuff on a UIButton that resides inside a uitableviewcell.
I've imported but the CALayer manipulation is not working.
When I moved it to cellForRowAtIndexPath method it works fine.
I do want to make the change once inside the class of the cell itself and not inside the table creation table.
Do you have any ideas ?
By the way, I do see the NSLog text on the init (so I know it entered the block).
Thanks !
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
NSLog(#"STARTED coder init in testResultCell");
[[self.bubbleMore layer] setMasksToBounds:YES];
[[self.bubbleMore layer] setBorderWidth:3.0f];
[[self.bubbleMore layer] setBorderColor:[UIColor blueColor].CGColor];
[[self.bubbleMore layer] setCornerRadius:20.0f];
}
return self;
}
Move the code to awakeWithNib. Your outlets won't be connected yet in initWithCoder. From Apple's documentation:
When an object receives an awakeFromNib message, it is guaranteed to have all its outlet instance variables set.
User interface objects are not available until viewDidLoad is called, which actually loads controls from your xib or Storyboard. Please move your code to viewDidLoad and it should work just fine.
If you add a breakpoint and look at the self.bubbleMore, it is going to be nil in initWithCode function.
Related
my didAddSubview is not being called.
This is what I am doing.
In my viewController
- (void)viewDidLoad {
[super viewDidLoad];
SomeView *view = [[SomeView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
[self.view addSubview:view];
}
Where my SomeView is a UI view (.h file)
#import <UIKit/UIKit.h>
#interface DyteMeetingView : UIView
#end
in which I have didAddSubview like this
- (void)didAddSubview:(UIView *)subview{
[super didAddSubview:subview];
[self someFunc];
}
I placed debugger in didAddSubview but the debugger never reaches here and hence [self someFunc]; is also not called.
Since didAddSubview is a lifecycle method for UI view, I am also not calling it from anywhere.
Can someone help me in figure out the concept I missed out on/what I could be doing wrong?
PS: I don't have any xib file for UIView.
PS: Intentionally added swift tag as well since there isn't large chunk of code and I am confident that swift developers would be able to understand the question/code and might be able to help me out.
didAddSubview in SomeView will be called when a sub view is added to an instance of SomeView, but that isn't what you are doing here. You are adding a SomeView as a subview of another view.
You want didMoveToSuperview; in this method you can use the superview property of self to identify the view that it moved to.
The didAddSubview will be called in viewController.view, instead of your SomeView, because you using viewController.view.addSubview(subview)
I have a customer UIView "CustomerUIView", and the CustomerUIView holds an instance of the UIView named "contentView". When an other view added as a subview of the instance of CustomerUIView, I want the view added as the subview of "contentView", so I override the addSubView method in CustomerUIView as below :
- (void)addSubview:(UIView *)view
{
[contentView addSubview:view];
}
But when I ran the program, it crashed somehow. So does that mean it is not correct to override the addSubview method?
As in my old code, many places has used the addSubview method, and that is the reason why I want to override that method instead of writing another method to use. Some advise?
It might be easier for the program to receive an alert when the subview is added instead, then you can run whatever code you need to then. See stackoverflow.com/questions/11364646/intercepting-didaddsubview
Alternatively, you could call try calling the super version of the method, and then running your own code, like:
- (void)addSubview:(UIView *)view {
[super addSubview:view];
[view removeFromSuperview];
[contentView addSubview:view];
}
I came across the exception 'NSInternalInconsistencyException' when I write the code below in a viewcontroller which is associated with storyboard:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.view.backgroundColor = [UIColor blackColor];
}
return self;
}
However, if I delete the line 'self.view.backgroundColor = [UIColor blackColor];', there is no problem. I don`t know the reason.
Actually, in the stroyboard I have several controllers, and I want to make a base controller for all the controllers. The purpose is that I want to set a background view(UIView) for them.
I try to add a UIView in the viewDidLoad method of the basecontroller, but the view covers any components that set in the storyboard. I think the reason is that the components on the sub viewcontrollers are initialized in the 'initWithCoder' method before the 'ViewDidLoad' method is called. So I try to add the bgView in the 'initWithCoder' method of the basecontroller, and this led to the problem above.
So I also want to know a right way to achieve my purpose. Thanks!
dont set graphics related properties in init and bring subview to front. Set them from
-(void)awakeFromNib
{
[self.view.superview bringSubviewToFront:self.view];
self.view.backgroundColor = [UIColor blackColor];
}
I am designing a custom view controller in interface builder with a XIB file and I have a custom UIView subclass that I want to add to my view controller. Here is how I've put my custom view into the view controller:
My TonerEffectButtonView class is a subclass of UIView (and has nothing to do with UIButton) and here is my code for it:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)prepareWithSelector:(SEL)selector onTarget:(id)targ withFilter:(GPUImageFilter*)filter{
self.gpuImageView = [[GPUImageView alloc] initWithFrame:self.frame];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:targ action:selector];
[self addGestureRecognizer:gesture];
selectedFilter = filter;
[filter addTarget:self.gpuImageView];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
}
initWithFrame: is not called anyway as I'm creating the view through a XIB, and I don't need any customization in initWithCoder: so I haven't implemented it. I have a GPUImageView (that is a part of a library that I'm using) that I'm creating programatically inside my view, and adding camera input to it (if you ask what happens in the prepare method). Prepare method is called from outside, and I've verified my UIView's frame is correct when it is called. I have a breakpoint on [super drawRect:rect]; and it never gets called, and when I run the program, my custom view is not displayed, nor does receive touch events (but prepare method DOES get called so the instance is created with the correct frame). It's like it has never been put there in interface builder. What could be the cause of this? I've seen many posts about this, and they mostly refer to creating views programatically and indicating problems with 'initWithFrame:'. But I have nothing to do with that method, and I want to use the interface builder/XIB couple, please don't advice me to create the view controller programatically. What could be the cause of it?
Thanks,
Can.
UPDATE: I can verify that -(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx is also not called in any way.
Ok, I've found the answer. First, I've tried to add another instance programatically, and I've found out that it has autoresizing mask set to none, whereas my original instance has it set to W+H. This clue made me realize that in fact, I was forgetting to add my GPUImageView instance as a subview into my custom view. I've added it, and it worked.
I have some custom appearance properties in my view class (a descendant of UIView). I want to customize the view appearance according to these properties, but I can’t do that inside the initializer, since the values set using [[MyClass appearance] setFoo:…] aren’t in effect at that point:
#interface View : UIView
#property(strong) UIColor *someColor UI_APPEARANCE_SELECTOR;
#end
#implementation View
#synthesize someColor;
// Somewhere in other code before the initializer is called:
// [[View appearance] setSomeColor:[UIColor blackColor]];
- (id) initWithFrame: (CGRect) frame
{
self = [super initWithFrame:frame];
NSLog(#"%#", someColor); // nil
return self;
}
#end
They are already set in layoutSubviews, but that’s not a good point to perform the view customizations, since some customizations may trigger layoutSubviews again, leading to an endless loop.
So, what’s a good point to perform the customizations? Or is there a way to trigger the code that applies the appearance values?
One possible workaround is to grab the value directly from the proxy:
- (id) initWithFrame: (CGRect) frame
{
self = [super initWithFrame:frame];
NSLog(#"%#", [[View appearance] someColor); // not nil
return self;
}
Of course this kills the option to vary the appearance according to the view container and is generally ugly. Second option I found is to perform the customizations in the setter:
- (void) setSomeColor: (UIColor*) newColor
{
someColor = newColor;
// do whatever is needed
}
Still I’d rather have some hook that gets called after the appearance properties are set.
Why not wait until
- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];
if (newSuperview) {
... code here ...
}
}
if it's giving you trouble?
I believe UIAppearance properties are applied to a view when it is being added into a view hierarchy. So presumably you could access the set properties in UIView didMoveToSuperview.
Caveat: I am using Swift 2, so not sure about earlier versions of Swift / Objective-C. But I have found that didMoveToSuperview() will not work. The properties are available in layoutSubviews(), but that's not a great place to do anything like this (since it can be called more than once). The best place to access these properties in the lifeCycle of the view I have found is didMoveToWindow().
I would have thought that viewDidLoad would be best if it's a one-time thing. Otherwise, viewWillAppear.
EDIT:
If you want to do it in the view, and not it's controller then I would create a custom init for the view along the lines of:
-(id) initWithFrame:(CGRect) frame andAppearanceColor:(UIColor)theColor;
thereby passing the colour into the view at creation time.