UI控件篇

苹果开发工具下载地址

https://developer.apple.com/download/more/

获取一张随机图片picsum.photos
切换终端对应的xcode,Xcode.app根据自己的名字修改
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
监听UITextField值的变化
[self.textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
设置按钮图片距右
button.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
静态字符串隐藏实现(仿系统)
// .h实现
typedef NSString * kCustomKey NS_EXTENSIBLE_STRING_ENUM;
UIKIT_EXTERN kCustomKey const _Nullable CustomKeyType ;
//.m实现
NSString * const CustomKeyType = @"custom_key_type";
Block
//局部block
NSAttributedString *(^createAttrStr)(NSString *, NSString *) = ^NSAttributedString *(NSString *a, NSString *b) {}
createAttrStr(@"数量",@"123");
@property(nonatomic,copy) void(^block)(NSString *string);//作为属性
- (void)failure:(nullable void (^)(NSError *error))failure;//作为方法参数
模拟器键盘切换隐藏和显示

command+k

屏幕旋转
self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
CGRect frame = [UIScreen mainScreen].bounds;
self.view.bounds = CGRectMake(0, 0, frame.size.height, frame.size.width);
更改图片的颜色

1.代码更改

UIImage *image = [[UIImage imageNamed:@"***"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
//button imageview 设置颜色后,设置tintColor
[view setTintColor:[UIColor redColor]];

2.代码更改
Assets.xcasset——>选择图片——>右上角(Render As)选择 Template Image
xib或者代码设置tintColor即可

更改图片的颜色

使用XIB设置
UITextField的Clear Button:属性为Appears while editing
如果用代码写,则设置:

textField.clearButtonMode = UITextFieldViewModeWhileEditing;
删除SceneDelegate

1.删除SceneDelegate的.h.m文件。
2.删除info.plist中的“Application Scene Manifest”项。
3.删除AppDelegate.m里面关于SceneDelegate的代码。
4.在AppDelegate.h中添加

@property (nonatomic, strong)UIWindow *window;
判断iPhone的刘海屏幕
#define Is_X \
({BOOL isPhoneX = NO;\
if (@available(iOS 11.0, *)) {\
isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;\
}\
(isPhoneX);})

// 导航栏高度
#define TopHeight (Is_X ? 88 : 64)
// TabBar栏高度
#define BottomHeight (Is_X ? (49 + 34) : 49)
//状态栏高度
#define NaviTopY (Is_X ? 44 : 20)
//TabBar底部安全区域
#define TabBottomY (Is_X ? 34 : 0)
取bundle图片
[UIImage imageNamed:@"***.bundle/imageName"];
UITableView自动高度
//代理返回
UITableViewAutomaticDimension
调用字符串方法
   SEL sel = NSSelectorFromString(methodName);
    if (sel) {
        IMP imp = [self methodForSelector:sel];
        if (imp) {
            void *(*func)(id,SEL) = (void *)imp;
            func(self,sel);
        }
    }
view跟随手指移动
   UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
   [moveView addGestureRecognizer:pan];

- (void)panAction:(UIPanGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:xView];//相对于xView的👈
    if(sender.state == UIGestureRecognizerStateChanged){
        CGFloat x = point.x;
        //根据需要更改坐标
    }
}

取消子view响应父view的手势
//方法1
    CGPoint point = [gestureRecognizer locationInView:self.bgView];
    BOOL result = [self.bgView.layer containsPoint:point];

//方法2 delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.bgView]) {
        return NO;
    }
    return YES;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容