12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Utility and Tips for iPhone programming

Last updated at Posted at 2013-03-29

Util

Get size of iPhone basic view

LayoutUtil.m
+(CGFloat)screenWidth
{
    CGRect r = [[UIScreen mainScreen] bounds];
    return r.size.width;
}

+(CGFloat)fullScreenHeight
{
    CGRect r = [[UIScreen mainScreen] bounds];
    return r.size.height;
}

+(CGFloat)screenHeight
{
    CGRect r = [[UIScreen mainScreen] applicationFrame];
    return r.size.height;
}

+(CGFloat)statusBarHeight
{
    CGRect full = [[UIScreen mainScreen] bounds];
    CGRect main = [[UIScreen mainScreen] applicationFrame];
    return full.size.height - main.size.height;
}

+(CGFloat)navigationBarHeight
{
    // Use this method in ViewController >> [[[self tabBarController] rotationgHeaderView] frame].size.height; 
    return 44.0;
}

+(CGFloat)tabBarHeight
{
    // Use this method in ViewController >>  [[[self tabBarController] rotationgFooterView] frame].size.height;
    return 49.0;
}

+(CGFloat)contentHeight
{
    return [LayoutUtil screenHeight] - [LayoutUtil navigationBarHeight];
}

+(CGFloat)contentY
{
    return [LayoutUtil statusBarHeight] + [LayoutUtil navigationBarHeight];
}

+(BOOL)isAspectRatio3x2
{
    return ([LayoutUtil fullScreenHeight] / [LayoutUtil screenWidth] == 3.0 / 2.0);
}

+(CGFloat)screenScale
{
    return ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) ? [[UIScreen mainScreen] scale] : 1.0;
}

Hex to UIColor (ex. @"FFAABBCC", @"AABBCC")

ColorUtil.m
+(UIColor*) hexToUIColor:(NSString *)hex
{
    if(hex.length == 6)
        hex = [@"FF" stringByAppendingString: hex];
	NSScanner *colorScanner = [NSScanner scannerWithString:hex];
	unsigned int color;
	[colorScanner scanHexInt:&color];
	CGFloat a = ((color & 0xFF000000) >> 24)/255.0f;
	CGFloat r = ((color & 0x00FF0000) >> 16)/255.0f;
	CGFloat g = ((color & 0x0000FF00) >> 8) /255.0f;
	CGFloat b =  (color & 0x000000FF) /255.0f;
	return [UIColor colorWithRed:r green:g blue:b alpha:a];
}

Prefix.pch

Define methods of LayoutUtil, you can use some definition such as IPHONE_WIDTH in your code

ProjectName-Prefix.pch
# import "LayoutUtil.h"
# define IPHONE_WIDTH [LayoutUtil screenWidth]
# define IPHONE_HEIGHT [LayoutUtil screenHeight]
# define IPHONE_FULL_HEIGHT [LayoutUtil fullScreenHeight]
# define IPHONE_STATUSBAR_HEIGHT [LayoutUtil statusBarHeight]
# define IPHONE_NAVIGATIONBAR_HEIGHT [LayoutUtil navigationBarHeight]
# define IPHONE_TABBAR_HEIGHT [LayoutUtil tabBarHeight]
# define IPHONE_CONTENT_HEIGHT [LayoutUtil contentHeight]
# define IPHONE_CONTENT_Y [LayoutUtil contentY]
# define IPHONE_SCREEN_SCALE [LayoutUtil screenScale]

Shorten name of NSLocalizedString >> _(@"hoge")

ProjectName-Prefix.pch
# define _(key) NSLocalizedString(key, key)

Category

Buttona factory

UIButton+Make.m
+ (UIButton *)makeButtonWithTarget:(id)target action:(SEL)action image:(UIImage *)image
{   
    UIButton *button = [[UIButton alloc] init];
    button.contentMode = UIViewContentModeScaleAspectFit;
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    
    image = nil;
    [image release];
    return button;
}

+ (UIButton *)makeButtonWithTarget:(id)target action:(SEL)action imageName:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
    return [self makeButtonWithTarget:target action:action image:image];
}

+ (UIButton *)makeButtonWithTarget:(id)target action:(SEL)action title:(NSString *)title
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.textColor = [UIColor blackColor];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
    [button setTitle:_(title) forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    return button;
}

Get size or width/height of view

You can use [viewA eY] instead of viewA.frame.origin.y + viewA.frame.size.height when ViewB is allocated at the bottom of ViewA.

UIView+Layout.m
- (CGFloat)sX
{
    return self.frame.origin.x;
}

- (CGFloat)sY
{
    return self.frame.origin.y;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (CGFloat)eX
{
    return self.frame.origin.x + self.frame.size.width;
}

- (CGFloat)eY
{
    return self.frame.origin.y + self.frame.size.height;
}

Get Google map logo view on MKMap

If the Google logo is hidden under the other views, the app does not pass inspection, it is necessary to move.

MKMapView+Logo.m
- (UIView*)getGoogleLogo
{
    NSArray *subviews = [self subviews];
    if (subviews && [subviews count] < 2) return nil;
    
    UIView *logoView = [[self subviews] objectAtIndex:1];
    if ([logoView isKindOfClass:[UIImageView class]] != YES) return nil;
    if (logoView.frame.size.width != 69 || logoView.frame.size.height != 23) return nil;
    return logoView;
}
12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?