您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

動手做一個漂亮的 iOS 登入頁:圓角、合併輸入框與自繪分隔線

編輯:iOS 相關資訊

Facebook 的登入頁很好看,其中一個關鍵細節是:Email 框和 Password 框是合在一起的,全體共用同一個圓角外框,中間只用一條細線分隔。

我們都知道,輸入框靠 layer.cornerRadius 就能做出圓角。但問題來了:如果上下兩個框各自加圓角,中間交界處會出現凹陷的缺口,看起來很糟。要怎麼只讓最上面有圓角?

廢話不多說,直接實戰。

步驟一:不要用 Storyboard,改回純程式碼

Xcode 新建的專案預設自帶故事板,操作起來不夠直觀。先把它拿掉,在 AppDelegate 裡手動建立視窗:

- (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;
}

記得同時把 Storyboard 的 Main Interface 設定清空,否則系統仍會去載入故事板。

步驟二:放上輸入框與按鈕

在 ViewController 的類別延伸中宣告屬性,並在 viewDidLoad 中建立:

@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 = @"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 = @"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

跑一下看結果——喔天哪,醜到哭。完全不能看。來美化一下。

步驟三:先加圓角試試

Apple 早就替我們想好了,只要加一行:

_account.layer.cornerRadius = 5.0;

執行起來確實好一點點,但還是很挫。因為接下來我們要把兩個輸入框合併,各自帶圓角的做法本來就走不通。

步驟四:用一個背景視圖把兩個框包起來

合併之後中間會有凹進去的部分,所以有兩種解法:

  1. 只為最上方那個邊角做圓角(需要處理特定角的 layer mask)。
  2. 整體墊一張帶圓角的背景視圖。

兩種都能達成,我們先用第二種,順便示範自繪。

先新增一個類別繼承 UIView,作為背景。之所以要自訂 UIView,是因為我們待會要用到它的繪圖方法 - (void)drawRect:(CGRect)rect。

在 ViewController 中修改:

_background = [[textFieldBackground alloc]
    initWithFrame:CGRectMake(20, 200, self.view.frame.size.width - 40, 100)];
[_background setBackgroundColor:[UIColor whiteColor]];
[[_background layer] setCornerRadius:5];
[[_background layer] setMasksToBounds:YES];
[self.view addSubview:_background];

_account = [[UITextField alloc]
    initWithFrame:CGRectMake(10, 0, self.view.frame.size.width - 40, 50)];
[_account setBackgroundColor:[UIColor clearColor]];
_account.placeholder = @"Email";
[_background addSubview:_account];

_password = [[UITextField alloc]
    initWithFrame:CGRectMake(10, 50, self.view.frame.size.width - 40, 50)];
[_password setBackgroundColor:[UIColor clearColor]];
_password.placeholder = @"Password";
[_background addSubview:_password];

關鍵是兩個輸入框的背景改成 clearColor,讓下面的白色圓角背景透出來。這樣圓角就只有外框那一層在處理,不必去湊四個角的交集。

又好看了一點,不過還是少了什麼——對了,中間少一條分隔線。這就是為什麼要自訂一個 UIView:現在終於可以用到它的繪圖方法了。

步驟五:在 drawRect: 裡畫分隔線

- (void)drawRect:(CGRect)rect {
    // 取得繪圖上下文
    CGContextRef context = UIGraphicsGetCurrentContext();

    // 設定線寬
    CGContextSetLineWidth(context, 0.2);

    // 開始繪製路徑
    CGContextBeginPath(context);

    // 移到起點
    CGContextMoveToPoint(context, 5, 50);

    // 畫到終點
    CGContextAddLineToPoint(context, self.frame.size.width - 5, 50);

    // 關閉路徑
    CGContextClosePath(context);

    // 設定顏色
    [[UIColor grayColor] setStroke];

    // 實際描邊
    CGContextStrokePath(context);
}

看效果——就是這樣,一個簡潔的登入頁完成了。

總結

這個登入頁用到的技術其實不多,重點主要在視覺設計這塊,加上一點點繪圖。主要步驟就是上面那七個 API 呼叫:

// 取得繪圖上下文
CGContextRef context = UIGraphicsGetCurrentContext();

// 設定粗細
CGContextSetLineWidth(context, 0.2);

// 開始繪圖
CGContextBeginPath(context);

// 移動到起點
CGContextMoveToPoint(context, 5, 50);

// 畫到第二個點
CGContextAddLineToPoint(context, self.frame.size.width - 5, 50);

// 關閉路徑
CGContextClosePath(context);

// 設定顏色
[[UIColor grayColor] setStroke];

// 描邊
CGContextStrokePath(context);

幾個可以再加強的地方

  • 用 Auto Layout 取代硬寫座標。上面所有 frame 都用絕對數字,換到不同尺寸的手機或旋轉時就會跑掉。改成 constraint 之後,畫面會自動適配。
  • 只圓特定角。若真的想讓「上框只有上兩角是圓的」,可以改用 UIBezierPath bezierPathWithRoundedRect:byRoundingCorners:cornerRadii: 搭配 CAShapeLayer 當 mask,比硬湊 cornerRadius 乾淨得多。
  • drawRect: 應該避免濫用。每次呼叫 setNeedsDisplay 都會觸發一次 CPU 繪圖。這裡畫一條線用 UIView 的 layer border 或一個 1pt 高的 UIView 子視圖會更省。drawRect: 留給真正需要客製路徑的场合。
  • 記得處理鍵盤與輸入體驗。returnKeyType、在 password 框設 secureTextEntry = YES、實作 keyboardType 與鍵盤收合,這些細節才是使用者實際感受到「好不好用」的地方。
  • 安全性提醒。登入頁不要自行把密碼存在 NSUserDefaults 裡「方便使用者」,該用 Keychain。這類為了便利性而犧牲安全性的做法,在真實專案裡是會被安全審查打回的。
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved