iOS9切片、提示框、InterfaceBuilder、多vc切

2020-10-08  本文已影响0人  我不白先生

原理:在图片中横向和纵向分别有两刀,其中四个角部分保持不变,中间的部分会复制或拉伸,从而构建比原图大的图片

@implementation UIImage (Creat9)
+(instancetype)imageWithName_9:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
    return [image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 10, 10)];
}

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //UIImage *image = [UIImage imageNamed:@"aec"];
    //UIEdgeInsetsMake(5,5,10,10)
    //上 左 下 右 的内边距
    //按 瓷块的方式 来适配中间放大的部分
//    UIImage *newImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 10, 10)];
//resizingMode可以选择用瓷块还是用拉伸
//     UIImage *newImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 10, 10)resizingMode:UIImageResizingModeStretch];
    //从 图片的 width Height  向后一像素进行拉伸
   // UIImage *newImage = [image stretchableImageWithLeftCapWidth:5 topCapHeight:5];
    UIImage *image_9 = [UIImage imageWithName_9:@"aec"];
    self.imageView.image = image_9;
}

屏幕呈现结果


9切片.png

改成下面的代码呈现的结果

+(instancetype)imageWithName_9:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
   // return [image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 10, 10)];
//从 图片的 width Height  向后一像素进行拉伸
   return [image stretchableImageWithLeftCapWidth:5 topCapHeight:5];
}
9切片2.png

UIAlertController

提示框
. alert 警告框
.actionSheet操作表

step1:使用工厂方法创建UIAlertController实例,同时指定样式是alert还是action sheet
step2:使用工厂方法创建UIAlertAction实例,代表创建按钮
step3:将按钮添加到alertController中,如果alert样式,则还可以添加文本框,action sheet不允许添加文本框
step4:使用控制器的presentViewController方法推出显示alertController

@implementation MyViewController
-(void)showAlert{
    //弹出对话框
    NSLog(@"点击了空白区域");
    
    //step1:使用工厂方法创建UIAlertController实例,同时指定样式是alert还是action sheet
    UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
    //step2:使用工厂方法创建UIAlertAction实例,代表创建按钮
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消登陆" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"重新登陆" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        if([alert.textFields[0].text isEqualToString:@"abc"]&&[alert.textFields[1].text isEqualToString:@"123"]){
            NSLog(@"登陆成功");
        }else{
            NSLog(@"登陆失败");
            [self showAlert]; //递归调用 直到登陆成功 退出方法
        }
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"确定删除该文件" style:UIAlertActionStyleDestructive handler:nil];
    //step3:将按钮添加到alertController中,如果alert样式,则还可以添加文本框,action sheet样式不允许添加文本框
    //step4:使用控制器的presentViewController方法推出显示alertController
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    //给警告框添加 文本框 操作表是不可以添加 文本框的
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请重新输入用户名";
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请重新输入密码";
    }];
    
    [self presentViewController:alert animated:YES completion:nil];

}
//view必须有背景色 该方法才会调用
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self showAlert];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
}

使用IB(Interface Builder)构建界面

将控制器中原本需要写大量代码进行构建界面的过程,通过一个后缀是xib的文件简化此操作。当再去创建控制器实例时,系统会通过xib文件,完成界面的创建
针对系统创建的这个视图,需要在控制器中访问时,可以通过连线的方式完成关联。

好久没用过单独创建xib文件,突然创建竟然发现程序崩溃的不行不行的,查找后知道怎么回事了,现在记录下来,以备不时之需

第一步:单独创建xib文件

image

第二步:点击File's Owner,按照图示输入对应的类名对应起来

image

第三步:点击选中File's Owner,按下cotrol键,同时拖动鼠标左键,指向View,建立连接

image

第四部:就可以按照加载xib文件的方法加载文件了

image

链接:https://www.jianshu.com/p/5605f8142559

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //bundle 文件夹 如果给nil 表示当前应用程序文件夹
    self.window.rootViewController = [[MyViewController alloc]initWithNibName:@"MyViewController" bundle:nil];
    //在xCode很早以前的版本,使用xib必须使用initWithNibName该初始化方法
    //但是现在 initWithNibName 可以,只用init方法也可以,如果使用init方法,会先在当前应用程序文件夹下,查找有没有和类文件名一样的xib文件,如果存在就通过xib来创建对象并加载
    [self.window makeKeyAndVisible];
    return YES;
}

多vc切换

多个ViewController之间的切换

step1: 将要推出的界面创建出创建出实例
step2:调用当前控制器的presentViewController方法退出要显示的VC

即可在推出B之后想从B返回到推出它的A:
step1:使用当前控制器的dismissViewCintroller方法即可
注意:A和B的生命周期:A推出B时,虽然看不见A了,但是A没有被销毁,从B返回A时,B会被销毁,A从隐藏状态变成显示状态

AViewController.m

@implementation AViewController
- (IBAction)goBVC:(id)sender {
    BViewController *BVC = [[BViewController alloc]init];
    [self presentViewController:BVC animated:YES completion:^{
        NSLog(@"跳转完成了");
    }];
}

BViewController.m

@implementation BViewController
- (IBAction)backAVC:(id)sender {
    //不需要指定回到哪个界面,也无法指定,只能回到前面的界面
     两个参数一个不是你要不要动画,第二表示回去以后做什么
    [self dismissViewControllerAnimated:YES completion:nil];
}
上一篇 下一篇

猜你喜欢

热点阅读