Tint a UIView with all its subviews - objective-c

Any way you can tint a UIView? Not the background color, but the entire UIView with all its subviews.
e.g - A UIView with an animation of a star rotating , i.e The UIView shape is constantly changing.

Eventually I created a UIView category that enables tinting of a UIView,
without filling the UIView rectangle, here it is :
Takes an image representation of the UIView, then colors it in the
given UIColor, this category was created to replicate a UIButton
default highlight behavior, so it also comes with a method that can activate that
behavior and let the category handle all touch methods.
//------------------ .h Interface file ------- //
//
// UIView UIView_Tint.h
// BabyQA
//
// Created by yogev shelly on 8/10/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#define tintColorClassicUIButton [UIColor colorWithWhite:0.0 alpha:0.5]
#interface UIView(Tint)
//proprties should not be used, use the methods decalred bellow
#property (nonatomic,retain) UIColor* tintColor;
#property(nonatomic,retain) UIImageView* tintImageView;
#property(nonatomic,assign) BOOL tintOnTouchActive;
-(void)tintToColor:(UIColor*)color;
-(void)clearTint;
-(void)enableTintOnTouchWithColor:(UIColor*)color;
-(void)disableTintOnTouch;
-(UIImage *)imageRepresentation;
-(UIImage*)imageRepresentationWithTintColor:(UIColor*)color;
#end
//------------------ .m Implementation file ------- //
// UIView UIView_Tint.m
// BabyQA
//
// Created by yogev shelly on 8/10/12.
// Copyright (c) 2012 __MyCompanyName__. All rights not reserved - go wild!
//
#import "UIView Tint.h"
#import <objc/runtime.h>
#import <QuartzCore/QuartzCore.h>
static char const * const tintImageViewKey = "tintImageView";
static char const * const tintColorKey = "tintColorKey";
static char const * const tintOnTouchActiveKey = "tintOnTouchActive";
#implementation UIView (Tint)
#dynamic tintImageView;
#dynamic tintColor;
#dynamic tintOnTouchActive;
-(void)enableTintOnTouchWithColor:(UIColor*)color
{
self.tintColor = color;
self.tintOnTouchActive = TRUE;
}
-(void)disableTintOnTouch
{
self.tintOnTouchActive = FALSE;
}
-(void)tintToColor:(UIColor*)color
{
if(![self.tintColor isEqual:color] || !self.tintImageView)
{
self.tintColor = color;
UIImage* tintImage = [self imageRepresentationWithTintColor:self.tintColor];
self.tintImageView = [[[UIImageView alloc] initWithImage:tintImage] autorelease];
}
[self addSubview:self.tintImageView];
}
-(void)clearTint
{
[self.tintImageView removeFromSuperview];
}
-(void)clearTintWithSecondsDelay:(float)delay
{
[self performSelector:#selector(clearTint) withObject:self afterDelay:delay];
}
#pragma mark - TouchToggling
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
if(self.tintOnTouchActive)
[self tintToColor:self.tintColor];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
if(self.tintOnTouchActive)
[self clearTintWithSecondsDelay:0.05];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
if(self.tintOnTouchActive)
[self clearTintWithSecondsDelay:0.05];
}
#pragma mark - TintingPart
-(UIImage *)imageRepresentation
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
-(UIImage*)imageRepresentationWithTintColor:(UIColor*)color
{
UIImage* viewImage = [self imageRepresentation];
viewImage = [self tintedImage:viewImage UsingColor:color];
return viewImage;
}
-(UIImage *)tintedImage:(UIImage*)image UsingColor:(UIColor *)tintColor {
UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);
CGRect drawRect = CGRectMake(0, 0, image.size.width, image.size.height);
[image drawInRect:drawRect];
[tintColor set];
UIRectFillUsingBlendMode(drawRect, kCGBlendModeSourceAtop);
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
#pragma mark - Dynamic setters/getters for Associative References
-(void)setTintImageView:(UIImageView *)tintImageView
{
objc_setAssociatedObject(self, tintImageViewKey, tintImageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIImageView*)tintImageView
{
return objc_getAssociatedObject(self , tintImageViewKey);
}
-(UIColor*)tintColor
{
return objc_getAssociatedObject(self , tintColorKey);
}
-(void)setTintColor:(UIColor *)tintColor
{
objc_setAssociatedObject(self, tintColorKey, tintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(BOOL)tintOnTouchActive
{
return [objc_getAssociatedObject(self, tintOnTouchActiveKey) boolValue];
}
-(void)setTintOnTouchActive:(BOOL)tintOnTouchActive
{
objc_setAssociatedObject(self, tintOnTouchActiveKey, [NSNumber numberWithBool:tintOnTouchActive], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#end

To tint a UIView, add another subview on top of it with the same frame and an alpha transparency set.
For example let's say your container type view is called containerView, you'd do as follows:
CGRect overlayFrame = containerView.frame;
UIView *overlayView = [UIView alloc] initWithFrame:overlayFrame];
overlayView.alpha = 0.5f;
overlayView.color = [UIColor blackColor];
[containerView addSubview:overlayView];
This gives you a semi transparent (0.5f) black tint that will give a tint like appearance to all the subviews underneath.

Related

How to draw Signature on UIView

I am new in ios.And I need to create a textview or label in which i can sign.
Like this image.
You can draw signature on UIView for that first subclass UIView and your subclass of UIView should be something like,
SignatureView.h
#import <UIKit/UIKit.h>
#interface SignatureView : UIView{
UIBezierPath *_path;
}
- (void)erase;
#end
SignatureView.m
#import "SignatureView.h"
#implementation SignatureView
- (void)drawRect:(CGRect)rect {
_path.lineCapStyle = kCGLineCapRound;
[_path stroke];
}
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame: frame];
if (self) {
[self setMultipleTouchEnabled: NO];
_path = [UIBezierPath bezierPath];
[_path setLineWidth:2.0];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[_path moveToPoint:[mytouch locationInView:self]];
[_path addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[_path addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
- (void)erase {
_path = nil; //Set current path nil
_path = [UIBezierPath bezierPath]; //Create new path
[_path setLineWidth:2.0];
[self setNeedsDisplay];
}
Then you can import SignatureView.h in any your view controller and can instantiate signature view something like,
SignatureView *signView= [[ SignatureView alloc] initWithFrame: CGRectMake(10, 10, self.view.frame.size.width-40, 200)];
[signView setBackgroundColor:[UIColor whiteColor]];
signView.layer.borderColor = [[UIColor lightGrayColor]CGColor];
signView.layer.borderWidth = 1.0;
[self.view addSubview:signView];
And on that view you can draw your signature!
And you can call erase method to erase the signature!
This my solution.
First I created the SignatureDrawView class.In side the SignatureDrawView class I wrote the function for the draw the signature.
SignatureDrawView.h
#import <UIKit/UIKit.h>
#interface SignatureDrawView : UIView
#property (nonatomic, retain) UIGestureRecognizer *theSwipeGesture;
#property (nonatomic, retain) UIImageView *drawImage;
#property (nonatomic, assign) CGPoint lastPoint;
#property (nonatomic, assign) BOOL mouseSwiped;
#property (nonatomic, assign) NSInteger mouseMoved;
- (void)erase;
- (void)setSignature:(NSData *)theLastData;
- (BOOL)isSignatureWrite;
#end
SignatureDrawView.m
#import "SignatureDrawView.h"
#implementation SignatureDrawView
#synthesize theSwipeGesture;
#synthesize drawImage;
#synthesize lastPoint;
#synthesize mouseSwiped;
#synthesize mouseMoved;
#pragma mark - View lifecycle
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithCoder:(NSCoder*)coder
{
if ((self = [super initWithCoder:coder]))
{
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self addSubview:drawImage];
self.backgroundColor = [UIColor whiteColor];
mouseMoved = 0;
}
return self;
}
#pragma mark touch handling
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches)
{
NSArray *array = touch.gestureRecognizers;
for (UIGestureRecognizer *gesture in array)
{
if (gesture.enabled & [gesture isMemberOfClass:[UISwipeGestureRecognizer class]])
{
gesture.enabled = NO;
self.theSwipeGesture = gesture;
}
}
}
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(!mouseSwiped)
{
UIGraphicsBeginImageContext(self.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
self.theSwipeGesture.enabled = YES;
mouseSwiped = YES;
}
#pragma mark Methods
- (void)erase
{
mouseSwiped = NO;
drawImage.image = nil;
}
- (void)setSignature:(NSData *)theLastData
{
UIImage *image = [UIImage imageWithData:theLastData];
if (image != nil)
{
drawImage.image = [UIImage imageWithData:theLastData];
mouseSwiped = YES;
}
}
- (BOOL)isSignatureWrite
{
return mouseSwiped;
}
#end
Next in ViewController I created the UIImageView with UIView.
ViewController.h
#import <UIKit/UIKit.h>
#import "SignatureDrawView.h"
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet SignatureDrawView *drawSignView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize drawSignView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)actionSave:(id)sender
{
// code for save the signature
UIGraphicsBeginImageContext(self.drawSignView.bounds.size);
[[self.drawSignView.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);
....Then do your stuff to save this in DB or server
}
- (IBAction)actionCancel:(id)sender
{
//code for cancel the signature
[self.drawSignView erase];
}
- (IBAction)actionClear:(id)sender
{
//code for clear the signature
[self.drawSignView erase];
}
#end
NOTE:When you set the view in xib first identity inspector(it is left
> side in utilities).Then click the drop down box of class(it is part of
Custom Class).Select or choose SignatureDrawView.After that hook up
the view from xib or storyboard to ViewController.h
Below is the output screenshots
Also

getting into the loop when doing progress view, ios

I am creating progress view by using CALayer by following
Header class
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface ProgressBar : UIView
#property (strong, nonatomic) CALayer *subLayer;
#property (nonatomic) CGFloat progress;
#end
Implementation class
#implementation ProgressBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.progress = 0;
self.backgroundColor = [UIColor whiteColor];
self.subLayer = [CALayer layer];
self.subLayer.frame = self.bounds;
self.subLayer.backgroundColor = [UIColor orangeColor].CGColor;
[self.layer addSublayer:self.subLayer];
}
return self;
}
- (void)setProgress:(CGFloat)value {
NSLog(#"what is going on here");
if (self.progress != value) {
self.progress = value;
[self setNeedsLayout];
}
}
- (void)layoutSubviews {
CGRect rect = [self.subLayer frame];
rect.size.width = CGRectGetWidth([self bounds]) * self.progress;
[self.subLayer setFrame:rect];
}
In my viewcontroller, I am doing
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.pBar = [[ProgressBar alloc] initWithFrame:CGRectMake(0, 50, 320, 10)];
[self.view addSubview:self.pBar];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)click:(id)sender {
[self.pBar setProgress:.8];
}
When (IBAction)click is fired, I am getting into the infinitive loop like the picture below
Does anyone know what I am getting into this loop. Please advice me on this issue. Thanks
The problem is that your setProgress: method calls the setProgress: method - recursion.
- (void)setProgress:(CGFloat)value {
NSLog(#"what is going on here");
if (self.progress != value) {
self.progress = value; // <-- This calls [self setProgress:value]
[self setNeedsLayout];
}
}
Change the bad line to:
_progress = value;
Setting the ivar directly is always required when you implement the setter method of a property.

UITextField shadow does not display when editing

I want to draw the text in an UITextField with a shadow. In order to do this, I have subclassed UITextField, and implemented the drawTextInRect: method as follows:
- (void)drawTextInRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
// Create shadow color
float colorValues[] = {0.21875, 0.21875, 0.21875, 1.0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef shadowColor = CGColorCreate(colorSpace, colorValues);
CGColorSpaceRelease(colorSpace);
// Create shadow
CGSize shadowOffset = CGSizeMake(2, 2);
CGContextSetShadowWithColor(context, shadowOffset, 0, shadowColor);
CGColorRelease(shadowColor);
// Render text
[super drawTextInRect:rect];
}
This works great for when the text field is not editing, but as soon as editing begins, the shadow disappears. Is there anything I am missing?
Here is the code for following component
#interface AZTextField ()
- (void)privateInitialization;
#end
#implementation AZTextField
static CGFloat const kAZTextFieldCornerRadius = 3.0;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (!self) return nil;
[self privateInitialization];
return self;
}
// In case you decided to use it in a nib
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (!self) return nil;
[self privateInitialization];
return self;
}
- (void)privateInitialization
{
self.borderStyle = UITextBorderStyleNone;
self.layer.masksToBounds = NO;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
self.layer.shadowOpacity = 0.5f;
self.layer.backgroundColor = [UIColor whiteColor].CGColor;
self.layer.cornerRadius = 4;
// This code is better to be called whenever size of the textfield changed,
// so if you plan to do that you can add an observer for bounds property
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:kAZTextFieldCornerRadius];
self.layer.shadowPath = shadowPath.CGPath;
}
#end
Couple of things to consider:
You want to set borderStyle to none, otherwise you'll end up with
UIKit putting subviews into your textfield
Depending on the Xcode
version you might want to link QuartzCore and to #import
<QuartzCore/QuartzCore.h>
For more complex appearance you can still
use shadow properties of the layer and move the drawing code itself
into the drawRect: method, but jake_hetfield was right if you
override drawRect you don't want to call super, especially in the end
of the method
As for the text drawing (you can see that it sticks to
close to the component borders), you have a separate
drawTextInRect: and drawPlaceholderInRect: method that draws the
text and placeholder respectively
You can use UIColor method for
colors and call CGColor property, it makes code more readable and
easier to maintain
Hope that helps!
Inspired by #jake_hetfield answer I created a custom UITextField that uses an internal label to do the drawing, check it out:
ShadowTextField .h file
#import <UIKit/UIKit.h>
#interface ShadowTextField : UITextField
// properties to change the shadow color & offset
#property (nonatomic, retain) UIColor *textShadowColor;
#property (nonatomic) CGSize textShadowOffset;
- (id)initWithFrame:(CGRect)frame
font:(UIFont *)font
textColor:(UIColor *)textColor
shadowColor:(UIColor *)shadowColor
shadowOffset:(CGSize)shadowOffset;
#end
ShadowTextField .m file
#import "ShadowTextField.h"
#interface ShadowTextField ()
#property (nonatomic, retain) UILabel *internalLabel;
#end
#implementation ShadowTextField
#synthesize internalLabel = _internalLabel;
#synthesize textShadowColor = _textShadowColor;
#synthesize textShadowOffset = _textShadowOffset;
- (id)initWithFrame:(CGRect)frame
font:(UIFont *)font
textColor:(UIColor *)textColor
shadowColor:(UIColor *)shadowColor
shadowOffset:(CGSize)shadowOffset
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// register to my own text changes notification, so I can update the internal label
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleUITextFieldTextDidChangeNotification)
name:UITextFieldTextDidChangeNotification
object:nil];
self.font = font;
self.textColor = textColor;
self.textShadowColor = shadowColor;
self.textShadowOffset = shadowOffset;
}
return self;
}
// when the user enter text we update the internal label
- (void)handleUITextFieldTextDidChangeNotification
{
self.internalLabel.text = self.text;
[self.internalLabel sizeToFit];
}
// init the internal label when first needed
- (UILabel *)internalLabel
{
if (!_internalLabel) {
_internalLabel = [[UILabel alloc] initWithFrame:self.bounds];
[self addSubview:_internalLabel];
_internalLabel.font = self.font;
_internalLabel.backgroundColor = [UIColor clearColor];
}
return _internalLabel;
}
// override this method to update the internal label color
// and to set the original label to clear so we wont get two labels
- (void)setTextColor:(UIColor *)textColor
{
[super setTextColor:[UIColor clearColor]];
self.internalLabel.textColor = textColor;
}
// override this method to update the internal label text
- (void)setText:(NSString *)text
{
[super setText:text];
self.internalLabel.text = self.text;
[self.internalLabel sizeToFit];
}
- (void)setTextShadowColor:(UIColor *)textShadowColor
{
self.internalLabel.shadowColor = textShadowColor;
}
- (void)setTextShadowOffset:(CGSize)textShadowOffset
{
self.internalLabel.shadowOffset = textShadowOffset;
}
- (void)drawTextInRect:(CGRect)rect {
// don't draw anything
// we have the internal label for that...
}
- (void)dealloc {
[_internalLabel release];
[_textShadowColor release];
[super dealloc];
}
#end
Here is how you use it in your view controller
- (void)viewDidLoad
{
[super viewDidLoad];
ShadowTextField *textField = [[ShadowTextField alloc] initWithFrame:CGRectMake(0, 0, 320, 30)
font:[UIFont systemFontOfSize:22.0]
textColor:[UIColor whiteColor]
shadowColor:[UIColor redColor]
shadowOffset:CGSizeMake(0, 1) ] ;
textField.text = #"This is some text";
textField.backgroundColor = [UIColor blackColor];
[self.view addSubview:textField];
}
You could try to do the drawing of the label yourself. Remove
[super drawTextInRect:rect]
And instead draw your own label. I haven't tried this but it could look something like this:
// Declare a label as a member in your class in the .h file and a property for it:
UILabel *textFieldLabel;
#property (nonatomic, retain) UILabel *textFieldLabel;
// Draw the label
- (void)drawTextInRect:(CGRect)rect {
if (self.textFieldLabel == nil) {
self.textFieldLabel = [[[UILabel alloc] initWithFrame:rect] autorelease];
[self.view addSubview:myLabel];
}
self.textFieldLabel.frame = rect;
self.textFieldLabel.text = self.text;
/** Set the style you wish for your label here **/
self.textFieldLabel.shadowColor = [UIColor grayColor];
self.textFieldLabel.shadowOffset = CGSizeMake(2,2);
self.textFieldLabel.textColor = [UIColor blueColor];
// Do not call [super drawTextInRect:myLabel] method if drawing your own text
}
Stop calling super and render the text yourself.
Have your tried with the standard shadow properties of the CALayer? it usually is enough and it is lot simpler. Try something like this with a regular UITextField:
self.inputContainer.layer.shadowColor=[UIColor blackColor].CGColor;
self.inputContainer.layer.shadowRadius=8.0f;
self.inputContainer.layer.cornerRadius=8.0f;
self.inputContainer.layer.shadowOffset=CGSizeMake(0, 4);
You need to import QuartzCore first of course!
#import <QuartzCore/QuartzCore.h>

Can I add custom UI Elements to UIActionSheet?

I've successfully added Buttons to my UIActionSheet. I was wondering if I could add other elements, adjust the opacity, and alter the direction of the panel flying in (default comes in from below)with my UIActionSheet?
I am relatively new to iOS programming so any help would be greatly appreciated.
If you’re going to customize it to that degree, you’re better off just creating your own custom overlay view and adding whatever controls you need to it. Adjusting the animation direction in particular would be more trouble than it’s worth.
For more information on custom views and the view hierarchy, plus a useful section on animations (see the sidebar), check out the View Programming Guide for iOS.
Write implemention!
//.h
#import <UIKit/UIKit.h>
#interface UIImageActionSheet : UIActionSheet {
UIImage *titleImage;
}
-(id) initWithImage:(UIImage *)image
title:(NSString *)title
delegate:(id <UIActionSheetDelegate>)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles;
#end
//.m file
#import "UIImageActionSheet.h"
#implementation UIImageActionSheet
-(id) initWithImage:(UIImage *)image
title:(NSString *)title
delegate:(id <UIActionSheetDelegate>)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles{
self = [super initWithTitle:title delegate:delegate
cancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:destructiveButtonTitle
otherButtonTitles:otherButtonTitles,nil];
if (self) {
titleImage=image;
[titleImage retain];
UIImageView *imageView = [[UIImageView alloc] initWithImage:titleImage];
imageView.frame = CGRectZero;
for (UIView *subView in self.subviews){
if (![subView isKindOfClass:[UILabel class]]) {
[self insertSubview:imageView aboveSubview:subView];
break;
}
}
[imageView release];
}
return self;
}
- (CGFloat) maxLabelYCoordinate {
// Determine maximum y-coordinate of labels
CGFloat maxY = 0;
for( UIView *view in self.subviews ){
if([view isKindOfClass:[UILabel class]]) {
CGRect viewFrame = [view frame];
CGFloat lowerY = viewFrame.origin.y + viewFrame.size.height;
if(lowerY > maxY)
maxY = lowerY;
}
}
return maxY;
}
-(void) layoutSubviews{
[super layoutSubviews];
CGRect frame = [self frame];
CGFloat labelMaxY = [self maxLabelYCoordinate];
for(UIView *view in self.subviews){
if (![view isKindOfClass:[UILabel class]]) {
if([view isKindOfClass:[UIImageView class]]){
CGRect viewFrame = CGRectMake((320 - titleImage.size.width)/2, labelMaxY + 10,
titleImage.size.width, titleImage.size.height);
[view setFrame:viewFrame];
}
else if(![view isKindOfClass:[UIImageView class]]) {
CGRect viewFrame = [view frame];
viewFrame.origin.y += titleImage.size.height+10;
[view setFrame:viewFrame];
}
}
}
frame.origin.y -= titleImage.size.height + 2.0;
frame.size.height += titleImage.size.height + 2.0;
[self setFrame:frame];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
}
*/
- (void)dealloc {
[super dealloc];
if (titleImage) {
[titleImage release];
}
}
#end
You have to create a custom overlay

UISplitViewController stops auto-rotating after trying to use the UINavigationController inside of it

I've been having a bit of trouble with a UISplitViewController in my iPad app. I am attempting to make a simple navigation tree using the UINavigationController inside of the UISplitView. I have used the following basic code to do this:
NavController.h
#interface NavController : NSObject {
/*
* This is connected properly to the UINavigationController in the
* UISplitViewController through Interface Builder.
*/
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
NavController.m
#import "NavController.h"
#implementation NavController
#synthesize navigationController;
- (void) awakeFromNib {
UIViewController *testController = [[UIViewController alloc] init];
UITableView *tableView = [[UITableView alloc] init];
[testController setView: tableView];
[navigationController pushViewController: testViewController
animated: YES];
}
#end
This code successfully pushes the view to the navigation controller, and I can navigate back with the back button, however, my problem arises with the fact that after this happens, my UISplitViewController no longer auto-rotates or rotates at all from the portrait position. When I remove this code (and the view does not get pushed) it works as expected.
What am I doing wrong, and am I going about this in the right way?
This drove me absolutely nuts too. I did a couple of things that made it work, but I'm not happy about my solutions -- 1) I don't really understand it and 2) it seems hacky.
I added this to my app delegate (my .m file):
#interface UITabBarController (MyApp)
#end
#interface UINavigationController (MyApp)
#end
#implementation UITabBarController (MyApp)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
#end
#implementation UINavigationController (MyApp)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
#end
This worked for the most part. For the views that didn't auto-rotate though, I had to manually rotate the views myself using transforms. I did something like:
- (void)deviceOrientationDidChangeWithAnimation:(BOOL)animated {
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == oldOrientation) {
return;
}
if (animated) {
CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationDidStopSelector:#selector(orientationChanged)];
}
[self sizeToFitOrientation:YES];
if (animated) {
[UIView commitAnimations];
}
oldOrientation = orientation;
}
- (CGAffineTransform)transformForOrientation {
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIInterfaceOrientationLandscapeLeft) {
return CGAffineTransformMakeRotation(M_PI*1.5); // rotated CCW
} else if (orientation == UIInterfaceOrientationLandscapeRight) { // CW
return CGAffineTransformMakeRotation(M_PI/2);
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { // CCW
return CGAffineTransformMakeRotation(-M_PI);
} else { // CW
return CGAffineTransformIdentity;
}
}
- (void)sizeToFitOrientation:(BOOL)transform {
if (transform) {
self.view.transform = CGAffineTransformIdentity;
}
CGRect frame = [UIScreen mainScreen].applicationFrame;
CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2));
CGFloat width = frame.size.width - 0 * 2;
CGFloat height = frame.size.height - 0 * 2;
UIInterfaceOrientation _orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(_orientation)) {
self.view.frame = CGRectMake(0, 0, height, width);
} else {
self.view.frame = CGRectMake(0, 0, width, height);
}
self.view.center = center;
if (transform) {
self.view.transform = [self transformForOrientation];
}
}
Hope this helps! And if someone can point out mistakes I've made (or bad things I'm perpetuating), I'd be happy to learn. :)