我们用Xcode开发APP的时候,首先要做的就是登录界面。那么如何在Xcode中制作APP登录界面呢?下面我给大家分享一下。
工具/材料XCode
01首先打开Xcode软件,新建一个Single View Application项目,如下图所示
02接下来在项目信息编写界面中设置一下项目信息,包括名称,语言,设备类型,如下图所示
03然后我们在APP的控件定义文件中声明好登录界面需要的控件,如下图所示
04接下来就需要在Load方法中对所有的控件都进行实例化,如下图所示,实例化的内容包括控件显示名称,位置,字体等信息
05实例化了所有控件以后,我们就需要把所有的控件都添加到APP的主界面视图中,通过如下图所示的addSubview方法添加
06设置完视图以后我们就可以点击左上角的运行按钮,如下图所示
07接下来我们就可以在软件中启动了手机的模拟器,然后显示出了登录界面,如下图所示
08最后我们实现一下登录方法即可,如下图所示,在pressLogin方法中验证输入的用户名和密码即可
xcode设置启动页有两种方式:
一.在LaunchScreen.storybord中创建imageview直接添加图片即可,只需要添加一张图片自适应所有机型。这是Xcode默认加载的启动页,也是优先提倡使用的方法。
注意事项:1.使用LaunchScreen.storybord加载启动页,图片文件不能放在Asset.xcassets里面,他会找不到图片名字导致加载不出来。图片需要放在工程目录下面。名字最好不要命名为LaunchImage,防止xcode容易混淆找不到图片路径,导致黑屏。
2.有时候会突然出现启动页展示不出来,造成黑屏现象。这时候需要清理一下缓存,把加载在LaunchScreen的图片更改名字重新运行一下。如果还是不行,删除图片文件在重新添加在工程里面,在改一下图片名字重新运行就可以了。
二.在Asset.xcassets里面创建LaunchImage添加各个机型需要的图片。这需要美工切图比较多。
1.在LaunchImage里面添加各个机型的图片
2.把Launch screen File的文件名清空
3.把LaunchScreen.storybord中的Use as Launch Screen前面对勾取消
4.在Asset Catalog launch Image Set name处设置LaunchImage
运行Xcode,启动页成功展示。
注意:设置LaunchImage,一定要在在Asset Catalog launch Image Set name处设置LaunchImage。否则启动页展示为黑屏。xcode11之后设置的位置发生变化,如图4.
现在xcode新建的项目都是自带故事板的, *** 作不是很方便,来把它改成说写代码
打开AppDelegate.h文件,添加以下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]self.window.rootViewController=[[ViewController alloc] init][self.window makeKeyAndVisible]return YES}
到此就完成了手写代码的第一步。
添加输入框和按钮
在ViewController.h中添加以下代码
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UITextField *account
@property (nonatomic,strong) UITextField *password
@property (nonatomic,strong) UIButton *loginButton
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]
[self.view setBackgroundColor:[UIColor colorWithRed:51/255.0 green:204/255.0 blue:255/255.0 alpha:1]]
_account=[[UITextField alloc] initWithFrame:CGRectMake(20, 200, self.view.frame.size.width-40, 50)]
_account.backgroundColor=[UIColor whiteColor]
_account.placeholder=[NSString stringWithFormat:@"Email"]
[self.view addSubview:_account]
_password=[[UITextField alloc] initWithFrame:CGRectMake(20, 260, self.view.frame.size.width-40, 50)]
_password.backgroundColor=[UIColor whiteColor]
_password.placeholder=[NSString stringWithFormat:@"Password"]
[self.view addSubview:_password]
_loginButton=[UIButton buttonWithType:UIButtonTypeRoundedRect]
[_loginButton setFrame:CGRectMake(20, 320, self.view.frame.size.width-40, 50)]
[_loginButton setTitle:@"Login" forState:UIControlStateNormal]
[_loginButton setBackgroundColor:[UIColor colorWithRed:51/255.0 green:102/255.0 blue:255/255.0 alpha:1]]
[_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]
[self.view addSubview:_loginButton]
}
@end
运行一下看看效果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)