UIView Doesn't Appear and drawRect Method Never Called - objective-c

I've made a UIView subclass called MiniView.
I try adding it to my viewController as follows:
#interface SomeViewController ()
#property (strong, nonatomic) MiniView *miniView;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
self.miniView = [[MiniView alloc] initWithFrame:CGRectMake(20.f, 20.f, 200.f, 200.f)];
_miniView.backgroundColor = [UIColor blackColor];
[self.view addSubview:_miniView];
}
The MiniView class looks like this:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSLog(#"DRAW RECT");
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor * redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
CGContextSetFillColorWithColor(context, redColor.CGColor);
CGContextFillRect(context, self.bounds);
}
But drawRect never gets called, unless I explicity call it, from within the setNeedsLayout method. It also doesn't draw anything. I've tried just adding a UIImageView in the drawRect method, and this appears fine. But the above code produces nothing.
I also get the error:
: CGContextSetFillColorWithColor: invalid context 0x0. This is
a serious error. This application, or a library it uses, is using an
invalid context and is thereby contributing to an overall degradation
of system stability and reliability. This notice is a courtesy: please
fix this problem. It will become a fatal error in an upcoming update.
If I print a log statement in the drawRect method and output the 'rect' values, it's correct at 200 x 200, so I don't know why the context is 0x0.
So one problem is that drawRect is never called, and the other problem is that if I call it explicitly, nothing is displayed...

You probably need to call setNeedsDisplay on your custom UIView subclass:
- (void)viewDidLoad {
[super viewDidLoad];
self.miniView = [[MiniView alloc] initWithFrame:CGRectMake(20.f, 20.f, 200.f, 200.f)];
_miniView.backgroundColor = [UIColor blackColor];
[_miniView setNeedsDisplay]; // Added this
[self.view addSubview:_miniView];
}
This is basically a prod to tell the system your UIView needs to be redrawn. See the docs for more info.

Posting as a separate answer as requested:
The actual problem was that I had redefined the setNeedsDisplay method in the MiniView class, like so:
- (void)setNeedsDisplay
{
NSLog(#"Redrawing info: %#", [_info description]);
}
Because I had neglected to make a call to [super setNeedsDisplay], drawRect was never being called and nothing was drawing. So this fixed it:
- (void)setNeedsDisplay
{
[super setNeedsDisplay];
NSLog(#"Redrawing info: %#", [_info description]);
}

Never call drawRect explicity, try this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.miniView = [[MiniView alloc] initWithFrame:CGRectMake(20.f, 20.f, 200.f, 200.f)];
_miniView.backgroundColor = [UIColor blackColor];
[self.view addSubview:_miniView];
[_miniView setNeedsDisplay];
}

Well in your drawrect method include this line:-
[super drawRect:rect];

Related

inserting an image in view or delegate

Right now my code was an app delegate, a view controller, and a main view
right now i'm just trying to insert a large image as the background, but i can't figure out where to put it and i'm getting an error when i run a simulation
<Error>: CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
- (void) applicationDidBecomeActive: (UIApplication *) application {
NSLog(#"AAAAAAAAAA");
[self startGameLoop];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"AAAAA");
mainView = [[MainView alloc] initWithFrame: [UIScreen mainScreen].applicationFrame];
mainView.backgroundColor = [UIColor grayColor];
[self.window addSubview: mainView];
[self.window makeKeyAndVisible];
[self startGameLoop];
return YES;
}
- (void) startGameLoop {
quack = [NSTimer scheduledTimerWithTimeInterval:.0303 target:self selector:#selector(loop) userInfo:nil repeats:YES];
NSLog(#"Game Loop timer instance: %#", quack);
mainView.backgroundColor = [UIColor grayColor];
}
- (void) loop {
NSLog(#"BBBBBB");
pic = [UIImage imageNamed:#"f1.png"];
[pic drawAtPoint:CGPointMake(160, 0)];
[mainView setNeedsDisplay];
}
- (void) drawRect: (CGRect) rect {
maps = [UIImage imageNamed:#"map.png"];
[maps drawAtPoint:CGPointMake(W/2, 0)];
}
You're calling a method ( [pic drawAtPoint:] ) that requires a "current" graphics context, outside of a graphics context. UIKit automatically creates one behind the scenes before any call to drawRect:.
Also, your drawRect must be in the MainView class, not within the UIApplicationDelegate.
Try removing [pic drawAtPoint:CGPointMake(160,0)] from within loop.
If you want to draw it every loop, it must be done within the drawRect method, within the MainView class.
Also, you mentioned you're using a view controller but I see none referenced in your code.
But if you only want to move an image around, you may want to consider NOT calling setNeedsDisplay. Rather, create a UIImageView, add it to your view, and setting its center property within the loop method.

UITableViewCell drawInRect iOS7

Hi I am trying to draw strings in my UITableViewCell in iOS 7 with the following code
-(void)drawRect:(CGRect)rect{
[super drawRect:rect];
CGRect playerNameRect = CGRectMake(0, kCellY, kPlayerNameSpace, kCellHeight);
NSDictionary*dictonary = [NSDictionary
dictionaryWithObjectsAndKeys:
[UIColor hmDarkGreyColor], NSForegroundColorAttributeName,
kFont, NSFontAttributeName,
nil];
[self.playerName drawInRect:playerNameRect withAttributes:dictonary];
}
However I can not get anything to appear... self.playerName is not nil, and the playerNameRect is correct.
I was previously using the following code to do the same thing but was recently deprecated in iOS 7
[self.playerName drawInRect:playerNameRect withFont:kFont lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentCenter];
What is also strange is I can not get anything to draw in drawRect on a UITableViewCell... The deprecated code works when I am drawingRect on just a UIView.
You shouldn't use UITableViewCell's drawRect method to perform custom drawing. The proper way to do it is to create a custom UIView and add it as a subview of your cell (as a subview of the contentView property). You can add the drawing code to this custom view and everything will work fine.
Hope this helps!
Check out these posts too:
Table View Cell custom drawing 1
Table View Cell custom drawing 2
Table View Cell custom drawing 3
As others said, don't use UITableViewCell's drawRect selector directly. By doing that, you're relying on implementation details of UITableViewCell, and Apple made no guarantee that such behaviour won't break in future versions, just as it did in iOS 7... Instead, create a custom UIView subclass, and add it as a subview to the UITableViewCell's contentView, like this:
#implementation CustomTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self.contentView addSubview:[[CustomContentView alloc]initWithFrame:self.contentView.bounds]];
}
return self;
}
#end
And the CustomContentView:
#implementation CustomContentView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSDictionary * attributes = #{
NSFontAttributeName : [UIFont fontWithName:#"Helvetica-bold" size:12],
NSForegroundColorAttributeName : [UIColor blackColor]
};
[#"I <3 iOS 7" drawInRect:rect withAttributes:attributes];
}
#end
Works like charm!
Try setting cell.backgroundColor = [UIColor clearColor] in init.
While I agree with the accepted answer, here's my take on it for the records:
If you don't need any of the builtin UITableViewCell functionality (swiping, removing, reordering, ...) and just use it as a container to draw your custom stuff, then you might want to consider removing all of the cells subviews in tableview:willDisplayCell:ForRowAtIndexPath. This will make your drawing be visible again and will get you maximum performance (since you get rid of the subviews you don't need).

Unbalanced calls to begin/end appearance transitions for <xxViewController: 0xaa82610>

I tried to add TapforTap Ads to my iPhone app, the ad appear but I show this message in console "Unbalanced calls to begin/end appearance transitions for ." after any navigation of pages and then the app crash, the below code for calling TapForTap Ads. How I can solve this problem?
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat y = self.view.frame.size.height - 50.0;
TapForTapAdView *adView = [[TapForTapAdView alloc] initWithFrame: CGRectMake(0, y, 320, 50) delegate: self];
[self.view addSubview: adView];
[TapForTapAppWall prepare];
[TapForTapAppWall showWithRootViewController: self]; // or possibly self.navigationController
}
Thanks A lot
I don't know it for sure. But try this:
- (void) ShowTapAd {
CGFloat y = self.view.frame.size.height - 50.0;
TapForTapAdView *adView = [[TapForTapAdView alloc] initWithFrame: CGRectMake(0, y, 320, 50) delegate: self];
[self.view addSubview: adView];
[TapForTapAppWall prepare];
[TapForTapAppWall showWithRootViewController: self]; // or possibly self.navigationController
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelectorOnMainThread:#selector(showTapAd) withObject:nil waitUntilDone:NO];
}
The run time warning that you got appears when you hussle with two (or more) segue, Navigation Controller push or present modally types of processes. Meaning when you initiate one before the former was totally finished.
I am surprised that those things can happen when you call something within viewDidLoad. So you may find out that the root cause is not located within the code sniplet that you have shown. But if it is then this could do the trick.
What it does:
It just makes sure that your setup of the ad view is performed after viewDidLoad is properly finished. As viewDidLoad runs on the main tread as all UI related stuff does (or should do), the current appearance transistion should be finised by then.
Hope, this helps. Again, it is just a guess.

Reading touch events in a QLPreviewController

I've got a QuickLook view that I view some of my app's documents in. It works fine, but I'm having my share of trouble closing the view again. How do I create a touch event / gesture recognizer for which I can detect when the user wants to close the view?
I tried the following, but no events seem to trigger when I test it.
/------------------------ [ TouchPreviewController.h ]---------------------------
#import <Quicklook/Quicklook.h>
#interface TouchPreviewController : QLPreviewController
#end
//------------------------ [ TouchPreviewController.m ]---------------------------
#import "TouchPreviewController.h"
#implementation TouchPreviewController
- (id)init:(CGRect)aRect {
if (self = [super init]) {
// We set it here directly for convenience
// As by default for a UIImageView it is set to NO
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleDoubleTap:)];
singleFingerDTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:singleFingerDTap];
[self.view setUserInteractionEnabled:YES];
[self.view setMultipleTouchEnabled:YES];
//[singleFingerDTap release];
}
return self;
}
- (IBAction)handleSingleDoubleTap:(UIGestureRecognizer *) sender {
CGPoint tapPoint = [sender locationInView:sender.view.superview];
[UIView beginAnimations:nil context:NULL];
sender.view.center = tapPoint;
[UIView commitAnimations];
NSLog(#"TouchPreviewController tap!" ) ;
}
// I also tried adding this
- (BOOL)gestureRecognizer:(UIGestureRecognizer *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherGestureRecognizer {
return YES;
}
#end
Edit: For clarification, this is how I instantiate the controller:
documents = [[NSArray alloc] initWithObjects: filename , nil ] ;
preview = [[TouchPreviewController alloc] init];
preview.dataSource = self;
preview.delegate = self;
//set the frame from the parent view
CGFloat w= backgroundViewHolder.frame.size.width;
CGFloat h= backgroundViewHolder.frame.size.height;
preview.view.frame = CGRectMake(0, 0,w, h);
//refresh the preview controller
[preview reloadData];
[[preview view] setNeedsLayout];
[[preview view] setNeedsDisplay];
[preview refreshCurrentPreviewItem];
//add it
[quickLookView addSubview:preview.view];
Also, I've defined the callback methods as this:
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return [documents count];
}
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index
{
return [NSURL fileURLWithPath:[documents objectAtIndex:index]];
}
Edit2: One thing i noticed. If I try making swiping gestures, I get the following message. This could shed some light on what is wrong/missing?
Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] since
gesture recognizer is not active.
I think your example code is incomplete. It isn't clear how you are instantiating the TouchPreviewController (storyboard, nib file or loadView.)
I have never used the class so I could be way out in left field.
If you've already instantiated a UITapGestureRecognizer in the parent viewController, it is absorbing the tap events and they aren't passed on to your TouchPreviewController.
I would implement the view hierarchy differently by attaching the UITapGestureRecognizer to the parent viewController and handle presentation and unloading of the QLPreviewController there.
I think you might not have to subclass QLPreviewController by instantiating the viewController from a nib file.
When your parent viewController's UITapGestureRecognizer got an event you would either push the QLPreviewController on the navigation stack or pop it off the navigation stack when done.
Hope this is of some help.

objective C: Buttons created from subclass of UIButton class not working

I am creating a subclass of UIButton in order to create my own customized buttons. My code as follows:
//interface file (subclass of uIButton
#interface UICustomButton : UIButton
{
Answer *answer;
NSString *btnType;
}
#property (nonatomic, retain) Answer *answer;
#property (nonatomic, assign) NSString *btnType;
- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame;
- (void)buttonPressed;
#end
//Implementation file (.m)
#implementation UICustomButton
#synthesize answer,btnType;
- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame;
{
self = [super initWithFrame:frame];
if (self)
{
self = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
self.backgroundColor = [UIColor colorWithHexString:#"#E2E4E7"];
}
[self addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlStateNormal];
self.answer = ans;
self.btnType = type;
return self;
}
I am facing some issues in getting the above code to work. I have 2 problems
1) The buttons are not responding to the selector method "buttonPressed"
2) I am hitting a runtime error for the lines 'self.answer = ans' and 'self.btnType = type' Stack trace as follows:
-[UIButton setAnswer:]: unrecognized selector sent to instance 0x614ebc0
2011-06-23 00:55:27.038 onethingaday[97355:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setAnswer:]: unrecognized selector sent to instance 0x614ebc0'
What am I doing wrong here?
This is happening because you are creating a UIButton type object and not a UICustomButton type inside the init method when you do
self = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
Try replacing your init method for
- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame;
{
self = [self initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
if (self)
{
self.backgroundColor = [UIColor colorWithHexString:#"#E2E4E7"];
[self addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
self.answer = ans;
self.btnType = type;
}
return self;
}
This will cause self to be a UICustomButton type object.
Also, you are using a wrong type for the UIControlState parameter when you add the target to your button using the addTarget:action:forControlEvents: method
You should use value among the ones bellow:
UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchCancel
EDIT:
Notes on UIButton subclassing
Many references on the web say you should NOT subclass the UIButton class, but not only anybody said why but what also deeply annoyed me was that the UIButton Class Reference does not say anything about it at all.
If you take UIWebView Class Reference for example, it explicitly states that you should not subclass UIWebView
Subclassing Notes The UIWebView class
should not be subclassed.
the big deal with UIButton is that it inherits from UIControl and a good and simple explanation is on the UIControl Class Reference itself
Subclassing Notes You may want to
extend a UIControl subclass for either
of two reasons:
To observe or modify the dispatch of
action messages to targets for
particular events
To provide custom
tracking behavior (for example, to
change the highlight appearance)
So, this means that you CAN subclass a UIButton, but you should be careful on what you are doing. Just subclass it to change its behavior and not its appearance. To modify a UIButton appearance you should use the interface methods provided for that, such as:
setTitle:forState:
setBackgroundImage:forState:
setImage:forState:
References worth reading
The UIView Programming Guide: View and Window Architecture -> Tips for Using Views Effectively -> Do Not Customize Controls by Embedding Subviews
Source: my post here
Not sure this was in the docs before, but anyway these are the current notes on + (id)buttonWithType:(UIButtonType)buttonType...
To me it looks like subclassing is OK as long as you use init instead of buttonWithType. I have yet to try it myself however.
Discussion This method is a convenience constructor for creating
button objects with specific configurations. It you subclass UIButton,
this method does not return an instance of your subclass. If you want
to create an instance of a specific subclass, you must alloc/init the
button directly.
When creating a custom button—that is a button with the type
UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0)
initially. Before adding the button to your interface, you should
update the frame to a more appropriate value.
If you want to get notifications when the user is interacting with your buttons, just sublcass UIButton and implement these methods:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesBegan");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesEnded");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesCancelled");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesMoved");
}
No init method required.
Edit
This answer reaches back several years, and things have changed - as Apple docs now explicitly mention subclassing and gives some hints.
So the following answer might be irrelevant or wrong for current development and might be ignored if you're interested in the current state of the art.
UIButton is not meant to be subclassed.
You are better off making a category and defining a factory method that delivers your needed button (with proper call to buttonWithType:). initWithFrame: is not the correct way to initialize a button anyway.
//
// BtnClass.m
#import "BtnClass.h"
#implementation BtnClass
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
//added custum properities to button
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(#"initWithCoder");
self = [super initWithCoder: aDecoder];
if (self) {
// Initialization code
_numberOfItems=[[UILabel alloc]initWithFrame:CGRectMake(40, 8, 160, 30)];
_numberOfItems.textAlignment=NSTextAlignmentLeft;
_numberOfItems.font = [UIFont boldSystemFontOfSize:18.0];
_numberOfItems.textColor = [UIColor darkGrayColor];
[self addSubview:_numberOfItems];
_leftImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 25, 25)];
[self addSubview:_leftImage];
_rightImage=[[UIImageView alloc]initWithFrame:CGRectMake(280, 10, 15, 15)];
[self addSubview:_rightImage];
[self setImage:[UIImage imageNamed:#"list-bg2-1.png"] forState:UIControlStateNormal];
[_rightImage setImage:[UIImage imageNamed:#"carat.png"]];
self.backgroundColor=[UIColor blackColor];
if(self.tag==1)
{
[_leftImage setImage:[UIImage imageNamed:#"notes-icon.png"]];
}
if(self.tag==2)
{
[_leftImage setImage:[UIImage imageNamed:#"photos-icon.png"]];
}
if(self.tag==3)
{
[_leftImage setImage:[UIImage imageNamed:#"videos-icon.png"]];
}
}
return self;
}
//selected method of uibutton
-(void)setSelected:(BOOL)selected
{
[super setSelected:selected];
if(selected)
{
[self setImage:nil forState:UIControlStateNormal];
_numberOfItems.textColor = [UIColor whiteColor];
[_rightImage setImage:[UIImage imageNamed:#"carat-open.png"]];
if(self.tag==1)
{
[_leftImage setImage:[UIImage imageNamed:#"white-notes-icon.png"]];
}
else if(self.tag==2)
{
[_leftImage setImage:[UIImage imageNamed:#"white-photo-icon.png"]];
}
else
{
[_leftImage setImage:[UIImage imageNamed:#"white-video-icon.png"]];
}
}
else{
_numberOfItems.textColor = [UIColor darkGrayColor];
if(self.tag==1)
{
[_leftImage setImage:[UIImage imageNamed:#"notes-icon.png"]];
}
if(self.tag==2)
{
[_leftImage setImage:[UIImage imageNamed:#"photos-icon.png"]];
}
if(self.tag==3)
{
[_leftImage setImage:[UIImage imageNamed:#"videos-icon.png"]];
}
[self setImage:[UIImage imageNamed:#"list-bg2-1.png"] forState:UIControlStateNormal];
[_rightImage setImage:[UIImage imageNamed:#"carat.png"]];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end