我们在project在,改变或多或少控件的坐标-宽度-高度,然后,经常看到你的self.view.frame.origin.x,self.view.frame.size.width.........相当的麻烦,在这里向大家推荐一个比較好的工具类,是UIView的类目,它里面对于求坐标,求高度什么的做了封装,非常方便大家调用.

@下载链接:点击这里

@.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @interface UIView (TTCategory) @property(nonatomic) CGFloat left;
@property(nonatomic) CGFloat top;
@property(nonatomic) CGFloat right;
@property(nonatomic) CGFloat bottom; @property(nonatomic) CGFloat width;
@property(nonatomic) CGFloat height; @property(nonatomic) CGFloat centerX;
@property(nonatomic) CGFloat centerY; @property(nonatomic,readonly) CGFloat screenX;
@property(nonatomic,readonly) CGFloat screenY;
@property(nonatomic,readonly) CGFloat screenViewX;
@property(nonatomic,readonly) CGFloat screenViewY;
@property(nonatomic,readonly) CGRect screenFrame; @property(nonatomic) CGPoint origin;
@property(nonatomic) CGSize size; @property(nonatomic) BOOL visible; /**
* Finds the first descendant view (including this view) that is a member of a particular class.
*/
- (UIView*)descendantOrSelfWithClass:(Class)cls; /**
* Finds the first ancestor view (including this view) that is a member of a particular class.
*/
- (UIView*)ancestorOrSelfWithClass:(Class)cls; /**
* Removes all subviews.
*/
- (void)removeAllSubviews; /**
* Calculates the offset of this view from another view in screen coordinates.
*/
- (CGPoint)offsetFromView:(UIView*)otherView; /**
* The view controller whose view contains this view.
*/
- (UIViewController*)viewController; - (void)addSubviews:(NSArray *)views; @end

@.m

#import "UIViewAdditions.h"

@implementation UIView (TTCategory)

/******************************view是否可见*******************************/
- (BOOL)visible{
return !self.hidden;
} - (void)setVisible:(BOOL)visible{
self.hidden = !visible;
} /******************************设置View左边x坐标*******************************/
- (CGFloat)left {
return self.frame.origin.x;
} - (void)setLeft:(CGFloat)x {
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
} /****************************设置View顶部y坐标*********************************/
- (CGFloat)top {
return self.frame.origin.y;
} - (void)setTop:(CGFloat)y {
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
} /****************************设置View右边x坐标*********************************/
- (CGFloat)right {
return self.frame.origin.x + self.frame.size.width;
} - (void)setRight:(CGFloat)right {
CGRect frame = self.frame;
frame.origin.x = right - frame.size.width;
self.frame = frame;
} /****************************设置View底部y坐标*************************************/
- (CGFloat)bottom {
return self.frame.origin.y + self.frame.size.height;
} - (void)setBottom:(CGFloat)bottom {
CGRect frame = self.frame;
frame.origin.y = bottom - frame.size.height;
self.frame = frame;
} /*****************************设置View的中心坐标********************************/
- (CGFloat)centerX {
return self.center.x;
} - (void)setCenterX:(CGFloat)centerX {
self.center = CGPointMake(centerX, self.center.y);
} - (CGFloat)centerY {
return self.center.y;
} - (void)setCenterY:(CGFloat)centerY {
self.center = CGPointMake(self.center.x, centerY);
} /**************************设置View的宽度***********************************/
- (CGFloat)width {
return self.frame.size.width;
} - (void)setWidth:(CGFloat)width {
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
} /*****************************设置View的高度********************************/
- (CGFloat)height {
return self.frame.size.height;
} - (void)setHeight:(CGFloat)height {
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
} - (CGFloat)screenX {
CGFloat x = 0;
for (UIView* view = self; view; view = view.superview) {
x += view.left;
}
return x;
} - (CGFloat)screenY {
CGFloat y = 0;
for (UIView* view = self; view; view = view.superview) {
y += view.top;
}
return y;
} - (CGFloat)screenViewX {
CGFloat x = 0;
for (UIView* view = self; view; view = view.superview) {
x += view.left; if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView* scrollView = (UIScrollView*)view;
x -= scrollView.contentOffset.x;
}
} return x;
} - (CGFloat)screenViewY {
CGFloat y = 0;
for (UIView* view = self; view; view = view.superview) {
y += view.top; if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView* scrollView = (UIScrollView*)view;
y -= scrollView.contentOffset.y;
}
}
return y;
} - (CGRect)screenFrame {
return CGRectMake(self.screenViewX, self.screenViewY, self.width, self.height);
} - (CGPoint)origin {
return self.frame.origin;
} - (void)setOrigin:(CGPoint)origin {
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
} - (CGSize)size {
return self.frame.size;
} - (void)setSize:(CGSize)size {
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
} - (CGPoint)offsetFromView:(UIView*)otherView {
CGFloat x = 0, y = 0;
for (UIView* view = self; view && view != otherView; view = view.superview) {
x += view.left;
y += view.top;
}
return CGPointMake(x, y);
}
/*
- (CGFloat)orientationWidth {
return UIInterfaceOrientationIsLandscape(TTInterfaceOrientation())
? self.height : self.width;
} - (CGFloat)orientationHeight {
return UIInterfaceOrientationIsLandscape(TTInterfaceOrientation())
? self.width : self.height;
}
*/
- (UIView*)descendantOrSelfWithClass:(Class)cls {
if ([self isKindOfClass:cls])
return self; for (UIView* child in self.subviews) {
UIView* it = [child descendantOrSelfWithClass:cls];
if (it)
return it;
} return nil;
} - (UIView*)ancestorOrSelfWithClass:(Class)cls {
if ([self isKindOfClass:cls]) {
return self;
} else if (self.superview) {
return [self.superview ancestorOrSelfWithClass:cls];
} else {
return nil;
}
} - (void)removeAllSubviews {
while (self.subviews.count) {
UIView* child = self.subviews.lastObject;
[child removeFromSuperview];
}
} - (UIViewController*)viewController {
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController*)nextResponder;
}
}
return nil;
} - (void)addSubviews:(NSArray *)views
{
for (UIView* v in views) {
[self addSubview:v];
}
}
@end @interface NSString (ParseCategory)
- (NSMutableDictionary *)explodeToDictionaryInnerGlue:(NSString *)innerGlue
outterGlue:(NSString *)outterGlue;
@end @implementation NSString (ParseCategory) - (NSMutableDictionary *)explodeToDictionaryInnerGlue:(NSString *)innerGlue
outterGlue:(NSString *)outterGlue
{
NSArray *firstExplode = [self componentsSeparatedByString:outterGlue];
NSArray *secondExplode; NSInteger count = [firstExplode count];
NSMutableDictionary* returnDictionary = [NSMutableDictionary dictionaryWithCapacity:count]; for (NSInteger i = 0; i < count; i++)
{
secondExplode =
[(NSString*)[firstExplode objectAtIndex:i] componentsSeparatedByString:innerGlue];
if ([secondExplode count] == 2)
{
[returnDictionary setObject:[secondExplode objectAtIndex:1]
forKey:[secondExplode objectAtIndex:0]];
}
}
return returnDictionary;
} @end

版权声明:本文博客原创文章,博客,未经同意,不得转载。

UIViewAdditions(一个非常方便的工具类用它)的更多相关文章

  1. java使用注解和反射打造一个简单的jdbc工具类

    a simple jdbc tools 如有转载和引用,请注明出处,谢谢 1. 定义我们需要的注解 要想实现对数据库的操作,我们必须知道数据表名以及表中的字段名称以及类型,正如hibernate 使用 ...

  2. java 写一个JSON解析的工具类

    上面是一个标准的json的响应内容截图,第一个红圈”per_page”是一个json对象,我们可以根据”per_page”来找到对应值是3,而第二个红圈“data”是一个JSON数组,而不是对象,不能 ...

  3. 使用POI做的一个生成Excel的工具类。包含了导出Excel和解析Excel方法

    PoiExcelUtils.java /** * */ package com.common.office; import java.io.File; import java.io.FileInput ...

  4. 翻翻git之---一个丰富的通知工具类 NotifyUtil

    转载请注明出处王亟亟的大牛之路 P1(废话板块.今天还加了个小广告) 昨天出去浪,到家把麦麦当当放出来玩一会就整到了12点多..早上睡过头了. .简直心酸. ... 近期手头上有一些职位能够操作,然后 ...

  5. 自己写了一个mysql连接的工具类【java】

    要用的话,包名自己可以改一下: package com.usa3v.dreamcenter.util; import java.sql.Connection; import java.sql.Driv ...

  6. 一个简单的Hibernate工具类HibernateUtil

    HibernateUtil package com.wj.app.util; import org.hibernate.Session; import org.hibernate.SessionFac ...

  7. Android开发之Toast吐司的一个封装好的工具类。带有源代码java文件,

    import android.content.Context; import android.widget.Toast; //Toast统一管理类 public class T { private T ...

  8. 一个md5加密的工具类,用的虚拟机的包,不需要额外导包

    package com.yun.park.service.utils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import jav ...

  9. Java一个文件上传工具类

    /** * 文件上传 * * @author cary * @since 2012-12-19 下午2:22:12 */ public class FileUploader { static fina ...

随机推荐

  1. C#新DataColumn类Type生成的方法类型参数

    DataColumn有的需要等级Type构造类型的参数,如以下: // // 摘要: // 使用指定列名称和数据类型初始化 System.Data.DataColumn 类的新实例. // // 參数 ...

  2. c++程序代写(qq:928900200)

     1. Both main memory and secondary storage are types of memory. Describe the difference between the  ...

  3. 【牛腩新闻公布系统】WebForms UnobtrusiveValidationMode 须要“jquery”ScriptResourceMapping。

    问题:       WebForms UnobtrusiveValidationMode须要"jquery"ScriptResourceMapping. 请加入一个名jquery ...

  4. PKI系统深入的介绍

    公钥基础设施(Public Key Infrastructure,缩写PKI)的基础与核心.是电子商务安全实施的基本保障.因此.对PKI技术的研究和开发成为眼下信息安全领域的热点. 本文对PKI技术进 ...

  5. 变化App.config其中值,并保存

    using System; using System.Collections.Generic; using System.Configuration; using System.IO; using S ...

  6. Div 滚动栏滚动到指定的位置

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Android 高仿微信即时聊天 百度云为基础的推

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/38799363 ,本文出自:[张鸿洋的博客] 一直在仿微信界面,今天最终有幸利用百 ...

  8. 图解Http协议 (转)

    一.技术基石及概述 问:什么是HTTP? 答:HTTP是一个客户端和服务器端请求和响应的标准TCP.其实建立在TCP之上的. 当我们打开百度网页时,是这样的: https://www.baidu.co ...

  9. hive的非交互模式

    在linux的终端运行:$HIVE_HOME/bin/hive 会进入交互模式: $HIVE_HOME/bin/hive  -e或者-f 是非交互模式 1.非交互模式运行HQL语句 $HIVE_HOM ...

  10. height:100%失败

    height显然,设置100% 为什么不能看到效果.非常多的时间不是很扎实的时间的基础上,,经常会遇到这样的问题,原因很简单的事实 首先,你必须确保 html{height:100%;} body{h ...