reasons why a UIButton with image doesn't always fire? - iphone

I'm at a loss.. I have a UIButton which almost works well.
98% of the time it fires the selector when pressed.
2% of the time it seems to get stuck for a while... about 5 seconds not accepting touches, then it magically starts working again.
This is how I declare it.. In the init of the parent frame:
CGRect frame = CGRectMake(0, 0, 320, VIEW_FRAME_HEIGHT);
self.frame = frame;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:BACKGROUND_ALPHA];
selectedLocationIndex = 0;
// create next/previous buttons
[nextButton release];
UIImage *nextImage = [IMagesClass getImage:IMAGE_KEY_NEXT];
CGRect nextFrame = CGRectMake(0, 0, nextImage.size.width + BUTTON_PADDING, nextImage.size.height + BUTTON_PADDING);
nextButton = [[UIButton alloc] initWithFrame:nextFrame];
nextButton.backgroundColor = [UIColor greenColor];
[nextButton setImage:nextImage forState:UIControlStateNormal];
[nextButton addTarget:delegate action:#selector(onChangeLocation:) forControlEvents:UIControlEventTouchUpInside];
nextButton.showsTouchWhenHighlighted = YES;
nextButton.tag = NEXT_LOCATION;
nextButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
nextButton.center = CGPointMake(self.frame.size.width - (BUTTON_INSET + (nextButton.frame.size.width / 2.0)), VIEW_FRAME_HEIGHT / 2.0);
[self addSubview:nextButton];
I appreciate any help! Thanks!

Related

UINavigationBar with two rounded corners

I have seen this answer
https://stackoverflow.com/a/9243472/563381
And while it works well visual as soon as you set the mask on the Navigation bar's layer it no longer responds to touches... So the back button that appears on the bar can't be clicked. Any solution to cause touches to go through a CALAyer? I didnt think CALayer's blocked touches or that a mask would ever block touches.
Well, I really don't know why CALayer blocks touches and this sounds odd to me...
The way I round corners of UINavigationBar consists in putting 2 UIImageView (10x10 pixels) in the corners and add 2 images to them. These images work as a mask, without blocking touches. If you use antialiasing to draw your images, the look is perfect.
You should try to use this code:
self.navigationController.navigationBar.translucent = YES;
This wil turn on your back button.You can see your button, but its in another layer. That's why it will not work the touches..
UPDATE:
Use this line of code for test. This will work like a charm for you.
//Style UINavigationBar
UIView *background = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
background.backgroundColor = [UIColor blackColor];
[self.view addSubview:background];
self.navigationController.navigationBar.tintColor = [UIColor cyanColor];
self.navigationController.navigationBar.translucent = YES;
CALayer *capa = [self.navigationController navigationBar].layer;
[capa setShadowColor: [[UIColor blackColor] CGColor]];
[capa setShadowOpacity:0.85f];
[capa setShadowOffset: CGSizeMake(0.0f, 1.5f)];
[capa setShadowRadius:2.0f];
[capa setShouldRasterize:YES];
//Round
CGRect bounds = capa.bounds;
bounds.size.height += 10.0f; //I'm reserving enough room for the shadow
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
[capa addSublayer:maskLayer];
capa.mask = maskLayer;
//Back Btn
UIButton *btnback = [UIButton buttonWithType:UIButtonTypeCustom];
[btnback setFrame:CGRectMake(0, 0, 54, 29)];
[btnback setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
UILabel * btnlabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 40, 23)];
btnlabel.backgroundColor = [UIColor clearColor];
btnlabel.textColor = [UIColor whiteColor];
btnlabel.font = [UIFont boldSystemFontOfSize:13];
btnlabel.text = #"back";
[btnback addSubview:btnlabel];
[btnback addTarget:self action:#selector(backHome:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btnback];

Text appears after hitting the return key in a UITextField

I have a custom class to create a textview with lines like the Notes app from Apple.
This is how the class looks:
NoteView.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface NoteView : UITextView <UITextViewDelegate> {
}
#end
NoteView.m
#import "NoteView.h"
#implementation NoteView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithRed:1.0f green:1.0f blue:0.6f alpha:1.0f];
self.font = [UIFont fontWithName:#"Marker Felt" size:20];
}
return self;
}
- (void)drawRect:(CGRect)rect {
//Get the current drawing context
CGContextRef context = UIGraphicsGetCurrentContext();
//Set the line color and width
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.2f].CGColor);
CGContextSetLineWidth(context, 1.0f);
//Start a new Path
CGContextBeginPath(context);
//Find the number of lines in our textView + add a bit more height to draw lines in the empty part of the view
NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / self.font.leading;
//Set the line offset from the baseline.
CGFloat baselineOffset = 6.0f;
//Iterate over numberOfLines and draw each line
for (int x = 0; x < numberOfLines; x++) {
//0.5f offset lines up line with pixel boundary
CGContextMoveToPoint(context, self.bounds.origin.x, self.font.leading*x + 0.5f + baselineOffset);
CGContextAddLineToPoint(context, self.bounds.size.width, self.font.leading*x + 0.5f + baselineOffset);
}
//Close our Path and Stroke (draw) it
CGContextClosePath(context);
CGContextStrokePath(context);
}
#end
I add the view as a subview. Everything works fine, but the text only apears after I hit the return key. I can see the flashing cursor, when I type I can see the cursor moving, but the text is gone... After hitting the return key, the text becomes visible and after that everything works fine. Even when I select all the text, remove it and start typing the text is visible.
This is how the textarea looks like:
How to solve this?
Thanks in advance!
UPDATE:
This is how I alloc the view:
annotateText = [[NoteView alloc] initWithFrame:CGRectMake(85.0, 168.0, 600.0, 567.0)];
annotateText.backgroundColor = [UIColor clearColor];
[annotateText setText:[annotationNotes objectAtIndex:0]];
[paperAnnotationView addSubview:annotateText];
I know this question is pretty old. I ran into the same issue and was looking for a solution. So I thought I would post the answer here, just in case, if any one comes and looks for it. After pulling my hair for hours, I figured out that the text view wont display the text in the very first line (unless we press enter/return or scroll the text) when the bounds of the textview is off the screen. Even when the parent view is off the screen, I ran into the same issue.
Here is the code that I was working on.
self.captionView = [[[UIImageView alloc] initWithFrame:CGRectMake(231, 769, 563, 643)] autorelease]; //63
self.captionView.image = [UIImage imageNamed:#"ekp006_preview_fbsharecontainer"];
self.captionView.userInteractionEnabled = YES;
//self.captionView.layer.contents = (id)[UIImage imageNamed:#"ekp006_preview_fbsharecontainer"].CGImage;
UIButton *cancelButton = [[[UIButton alloc] initWithFrame:CGRectMake(10, 10, 73, 34)] autorelease];
[cancelButton setImage:[UIImage imageNamed:#"ekp006_preview_fbshare_btn_cancel"] forState:UIControlStateNormal];
[cancelButton setImage:[UIImage imageNamed:#"ekp006_preview_fbshare_btn_cancel_down"] forState:UIControlStateHighlighted];
[cancelButton addTarget:self action:#selector(cancelFacebookShare) forControlEvents:UIControlEventTouchUpInside];
[self.captionView addSubview:cancelButton];
UIButton *shareButton = [[[UIButton alloc] initWithFrame:CGRectMake(480, 10, 73, 34)] autorelease];
[shareButton setImage:[UIImage imageNamed:#"ekp006_preview_fbshare_btn_share"] forState:UIControlStateNormal];
[shareButton setImage:[UIImage imageNamed:#"ekp006_preview_fbshare_btn_share_down"] forState:UIControlStateHighlighted];
[shareButton addTarget:self action:#selector(facebookshare) forControlEvents:UIControlEventTouchUpInside];
[self.captionView addSubview:shareButton];
self.captionTextView = [[[UITextView alloc] initWithFrame:CGRectMake(20, 60, 523, 100)] autorelease];
self.captionTextView.textColor = [UIColor lightGrayColor];
self.captionTextView.delegate = self;
self.captionTextView.layer.shadowRadius = 5.0;
self.captionTextView.layer.shadowColor = [UIColor blackColor].CGColor;
self.captionTextView.layer.shadowOpacity = 0.8;
self.captionTextView.layer.borderColor = [UIColor blackColor].CGColor;
self.captionTextView.layer.borderWidth = 0.75;
self.captionTextView.layer.cornerRadius = 5.0f;
self.captionTextView.font = [UIFont fontWithName:#"VAGRoundedBlackSSi" size:18];
[self.captionView addSubview:self.captionTextView];
[[self topMostViewController].view addSubview:self.captionView];
If you see closely, the parent view for me (the caption view) was 769 in y origin and I made this explicit, so that I can make the view slide from bottom to top using UIView animations. In order to solve this, I had to take the alternate path of making the parent view frame as CGRectMake(231,63,563,643) and hide the entire view. And to present it I used something like this.
- (void) presentCaptionCaptureScreen
{
// The code which works
[AnimationEffects bounceAnimationForView:self.captionView afterTimeInterval:0];
self.captionTextView.textColor = [UIColor lightGrayColor];
self.captionTextView.text = #"Enter your caption for the photo";
// The code which didn't work
/*
[UIView animateWithDuration:0.7 animations:^
{
self.captionTextView.text = #"";
self.captionView.frame = CGRectMake(231, 63, 563, 643);
} completion:^(BOOL finished)
{
self.captionTextView.textColor = [UIColor lightGrayColor];
self.captionTextView.text = #"Enter your caption for the photo";
}];
*/
}
It worked for me!

UIImageView doesn't follow origin.x position

I am trying to add set of imageviews on a UIScrollView. My problem is that the imageviews are overlapping with each other even though I increase the x value. What's also weird is that this problem is only for devices with ios 4.2.1, but works on simulator 4.2 and other device ios.
Here is my code:
- (void)addScrollView {
[self setImages];
scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
scrollView.delegate = self;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
[self addSubview:scrollView];
NSUInteger nimages;
CGFloat cx = 0;
for (nimages = 1; nimages < [imagesArray count] ; nimages++) {
UIImage *image = [UIImage imageNamed:[imagesArray objectAtIndex:nimages]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.origin.x = cx;
rect.origin.y = 0;
rect.size.width = 320;
rect.size.height = 480;
imageView.frame = rect;
[scrollView addSubview:imageView];
[imageView release];
cx += rect.size.width;
}
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}
Called from here:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self addScrollView];
UIImage *handImg;
UIImageView *longhand = [[UIImageView alloc] init];
minuteHand = [[UIView alloc] initWithFrame:CGRectMake(0, 55, 320, 480)];
handImg = [UIImage imageNamed:#"clock_long_hand.png"];
longhand.image = handImg;
/************** TRY COMMENT OUT HERE **************/
longhand.frame = CGRectMake(155 - (handImg.size.width / 2), 240 - handImg.size.height, handImg.size.width, handImg.size.height);
[minuteHand addSubview:longhand];
[self addSubview:minuteHand];
/************** END COMMENT OUT HERE **************/
[longhand release];
}
return self;
}
UPDATE:
This code is in a UIView subclass which is why I use [self...] instead of [self.view...].
I tried experimenting and found that when I comment out the indicated parts, the scrollview works fine. But when it is included, this is when the imageviews are overlapped.
I know that the scrollview's content size is correct because its indicator continues to scroll, with just a plain black background.
I am really confused why this works on some ios and not on others. Hope you can help.
This might have something to do with autoresizing mask. The width actual images, I mean .png files is bigger than 320, right?

how to resize the button image in iphone

im new in iphone.now im developing button image view in iphone.here images are images displaying large size i want to resize the images. that means images are displayed like medium size. i implement code like this.
images = [[NSMutableArray alloc]init];
[images addObject:[UIImage imageNamed:#"bearlarge.jpg"]];
[images addObject:[UIImage imageNamed:#"bufflo_large.jpg"]];
[images addObject:[UIImage imageNamed:#"camel_large.jpg"]];
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
int row = 0;
int column = 0;
for(int i = 0; i < images.count; ++i) {
// UIImage *thumb = [images objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(column*105+14, row*105+10, 50,50);
[button setImage:[images objectAtIndex:i] forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[view addSubview:button];
if (column == 2) {
column = 0;
row++;
} else {
column++;
}
}
//[view setContentSize:CGSizeMake(320, (row+1) * 80 + 10)];
self.view = view;
[view release];
can any one plz send me code for how to resize the button images in iphone.
Thank you in advançe.
You could create this category for UIImage:
- (UIImage *)rescaleImageToSize:(CGSize)size {
CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
[self drawInRect:rect]; // scales image to rect
UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resImage;
}
Better add a UIImageView over the button. So that you can easily change the frame size.
CODE
UIButton *imageBut=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
img.userInteractionEnabled=NO;
[imageBut addSubview:img];
[self.view addSubview:imageBut];
[imageBut release];
[img release];
Or else, use
[button setBackgroundImage:YourImage];
setImage will set the image with original size in which you are included that into your project.
setBackgroundImage will resize the original image.
[button setBackgroundImage:[images objectAtIndex:i] forState:UIControlStateNormal];
-(void)scaleImage:(id)sender
{
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}

Change background color of UIButton when Highlighted

Hi I want to change the UIButton background color which user Tap on the button.
I am showing background color using gradient, when user tap I need to change the gradient color.
[btn setTitle: #"Next" forState:UIControlStateNormal];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = btn.bounds;
gradient.cornerRadius = 10.0f;
locations = [[NSArray alloc] initWithObjects: 0.0f, 0.0f, 0.0f,0.0f, nil];
[gradient setLocations:locations];
colorNext = [[NSArray alloc] initWithObjects:…., nil];
gradient.colors = colorNext;
[locations release];
[btn.layer insertSublayer:gradient atIndex:0];
btn.titleLabel.textColor = [UIColor blackColor];
Changing the internal layer hierarchy of a UIButton is not recommended since it may break in a future update of the iOS. Also, it may cause Apple to reject your application. A better solution would be to create a button subclass. Here is an example of how to do it:
#interface MyButton : UIButton
#end
#implementation MyButton
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[self addObserver:self forKeyPath:#"highlighted" options:NSKeyValueObservingOptionNew context:NULL];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
if (self.highlighted == YES)
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
const CGFloat *topGradientColorComponents = CGColorGetComponents([UIColor whiteColor].CGColor);
const CGFloat *bottomGradientColorComponents = CGColorGetComponents([UIColor blackColor].CGColor);
CGFloat colors[] =
{
topGradientColorComponents[0], topGradientColorComponents[1], topGradientColorComponents[2], topGradientColorComponents[3],
bottomGradientColorComponents[0], bottomGradientColorComponents[1], bottomGradientColorComponents[2], bottomGradientColorComponents[3]
};
CGGradientRef gradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors) / (sizeof(colors[0]) * 4));
CGColorSpaceRelease(rgb);
CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0, 0), CGPointMake(0, self.bounds.size.height), 0);
CGGradientRelease(gradient);
}
else
{
// Do custom drawing for normal state
}
}
- (void)dealloc
{
[self removeObserver:self forKeyPath:#"highlighted"];
[super dealloc];
}
#end
You may need to modify it a bit to get it to do what you want but I think you get the basic idea.
Try this:
[Button addTarget:self action:#selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
[Button addTarget:self action:#selector(setBgColorForButton:) forControlEvents:UIControlEventTouchDown];
[Button addTarget:self action:#selector(clearBgColorForButton:) forControlEvents:UIControlEventTouchDragExit];
-(void)setBgColorForButton:(UIButton*)sender
{
[sender setBackgroundColor:[UIColor blackColor]];
}
-(void)clearBgColorForButton:(UIButton*)sender
{
[sender setBackgroundColor:[UIColor redColor]];
}
-(void)doSomething:(UIButton*)sender
{
double delayInSeconds = 0.3;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[sender setBackgroundColor:[UIColor redColor]];
});
//do something
}
You can use this method to create and solid UIImage of specified color and then apply it to your button. I've made a category for UIImage to do this.
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, (CGRect){.size = size});
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *)imageWithColor:(UIColor *)color
{
return [UIImage imageWithColor:color size:CGSizeMake(1, 1)];
}
Usage:
[button setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]
forState:UIControlStateNormal];
As Matthias pointed out, you can safely use 1x1 image for your button background – it will be stretched to fill the button.
I found this AYUIButton on GitHub and it works perfectly :)
Here is sample code:
[btn setBackgroundColor:[UIColor redColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor blueColor] forState:UIControlStateHighlighted];
When you set the TintColor of the Button it works when highlighted
[YourButton setTintColor:[UIColor color]];
Swift
Extension for UIButton that let you specify background color for each state:
https://github.com/acani/UIButtonBackgroundColor
Please note that for iphone 6 plus you should change the following, inside the extension in the .swift class, that will fix it for all the versions:
//let rect = CGRect(x: 0, y: 0, width: 0.5, height: 0.5) comment this line
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)