[英]UITextField - Keyboard not showing
我在其中有一個父視圖控制器,我有一個自定義視圖,初始化此自定義視圖時,我設置了另一個自定義視圖,並且該視圖具有UITextField,但是當我單擊UITextfield時,鍵盤從不顯示。
ViewController:
//
// ViewController.m
// InstantForum
//
// Created by trikam patel on 27/08/2014.
// Copyright (c) 2014 trikam patel. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize loginSignupControlView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.userInteractionEnabled = YES;
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
self.loginSignupControlView = [[LoginSignupControlView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 68)];
[self.view addSubview:self.loginSignupControlView];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
自定義視圖,其中包含另一個包含UITextFields的自定義視圖
//
// LoginSignupControlView.m
// InstantForum
//
// Created by trikam patel on 28/08/2014.
// Copyright (c) 2014 trikam patel. All rights reserved.
//
#import "LoginSignupControlView.h"
#import "UIHelper.h"
#import "TextField.h"
@implementation LoginSignupControlView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createView];
}
return self;
}
-(void)createView{
self.isLogin = true;
self.userInteractionEnabled = YES;
self.backgroundColor = [UIColor colorWithRed:56.f/255 green:56.f/255 blue:57.f/255 alpha:1.0f];
self.logoLabel = [UIHelper getUILabel:CGRectMake(10,35, 200, 14) :@"INSTANT FORUM" :[UIColor colorWithRed:222.0f/255 green:221.0f/255 blue:221.0f/255 alpha:1.0f] :[UIFont fontWithName:@"Helvetica" size:14]];
[self addSubview:self.logoLabel];
self.loginLabel = [UIHelper getUILabel:CGRectMake(190,35, 50, 14) :@"LOGIN" :[UIColor colorWithRed:106.0f/255 green:196.0f/255 blue:229.0f/255 alpha:1.0f] :[UIFont fontWithName:@"Helvetica" size:14]];
[self addSubview:self.loginLabel];
self.signupLabel = [UIHelper getUILabel:CGRectMake(250,35, 60, 14) :@"SIGNUP" :[UIColor colorWithRed:106.0f/255 green:196.0f/255 blue:229.0f/255 alpha:1.0f] :[UIFont fontWithName:@"Helvetica" size:14]];
[self addSubview:self.signupLabel];
[self setupView];
}
-(void)setupView{
if(self.isLogin){
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
self.loginView = [[LoginView alloc] initWithFrame:CGRectMake(0, 68, screenRect.size.width, 286)];
[self addSubview:self.loginView];
self.imageViewFN = [UIHelper getUIImageView:CGRectMake(10, 10, 300, 35) :@"textbox1.png"];
[self.loginView addSubview:self.imageViewFN];
TextField *fnText = [[TextField alloc] initWithFrame:CGRectMake(20, 10, 280, 35)];
//fnText.backgroundColor = [UIColor clearColor];
fnText.text = @"Enter First Name";
fnText.userInteractionEnabled = YES;
fnText.enabled = YES;
fnText.font = [UIFont fontWithName:@"Helvetica" size:14];
fnText.textColor = [UIColor colorWithRed:105.0f/255 green:121.0f/255 blue:221.0f/255 alpha:1.0f];
[self.loginView addSubview:fnText];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
自定義視圖,LoginView
//
// LoginView.m
// InstantForum
//
// Created by trikam patel on 28/08/2014.
// Copyright (c) 2014 trikam patel. All rights reserved.
//
#import "LoginView.h"
@implementation LoginView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithRed:61.0f/255 green:81.0f/255 blue:168.0f/255 alpha:1.0f];
self.userInteractionEnabled = YES;
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
原因之一可能是UITextField上缺少明確的委托。
將其UITextField實例的委托設置為父容器(將其添加到的父容器),這將使委托和UITextField能夠偵聽更改。 然后,您可以進一步推動信封,並在初始化容器視圖時嘗試使UITextField聚焦:
self.tfText.delegate = self; // too many selfs for clarity purpose
然后可以在父視圖中實現以下內容:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
最后,從代碼角度來看,如果LoginSignupViewController不需要用戶輸入,只需將用戶交互設置為NO,並僅在需要這樣做的視圖中設置標志(在本例中為UITextField的容器)即可。
另一個愚蠢的原因,如果您正在模擬器中運行應用程序,則可能是以下原因:
“當您的iOS模擬器打開時,轉到(頂部菜單):
硬件>鍵盤>取消選中類似“模擬硬件鍵盤”之類的字詞
做完了!
希望對您有所幫助
鍵盤不顯示可能有幾個原因。 首先確保您的UITextField在筆尖和代碼中都是交互式的(UITextField本身和在Superview層次結構中應該是交互式的)。
接下來,您必須確保UITextfield的SuperViews足夠大,以便UITextfield保持交互性。 您可以為所有視圖賦予不同的背景顏色,看看是否有重疊的顏色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.