I added a text field in viewDidLoad but it did not show up on screen.
Here's .h
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController{
UITextField *tfText;
}
#property (nonatomic, retain) UITextField *tfText;
#end
Here's .m
- (void)viewDidLoad{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor lightGrayColor]];
tfText.frame = CGRectMake(65, 100, 200, 50);
tfText.backgroundColor = [UIColor whiteColor];
[tfText setTextColor:[UIColor blackColor]];
tfText.placeholder = #"Test";
[tfText setBorderStyle:UITextBorderStyleNone];
[self.view addSubview:tfText];
}
seems that you need to initialize the object... I mean
UITextField *newTextField = [[UITextField alloc] init];
self.tfText = newTextField;
//....
//all your code here
//....
[newTextField release];
And dont forget to release your instance on dealloc method.
D33pN16h7 is correct, you need to instantiate your object. However I would do it a little differently than described above, there is no reason to create a UITextField instance, and then set your instance to it.
self.tfText = [[UITextField alloc] init];
UITextField entered2 = [[UITextField alloc] initWithFrame:CGRectMake(120.0, 125.0, 150.0, 25.0)];
[entered2 setBackgroundColor:[UIColor whiteColor]];
entered2.text=#"Active";
[self.view addSubview:entered2];
[entered2 release];
Related
I'd like to be able to create elements (like UIViews) when for example the user touches a button
NSMutableString *myVar = [NSMutableString stringWithFormat:#"_view%i", num];
UIView * newView = [self valueForKey:myVar];
but without adding all the
UIView * _view1;
UIView * _view2;
...
in the .h file (if only this is possible..)
You can use an NSMutableArray to hold them. Each time you create a new view just add it to the array.
Here's sample code that should do what you want.
#interface MyViewController ()
#property (strong, nonatomic) NSMutableArray *listChildViews;
#end
#implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.listChildViews = [[NSMutableArray alloc] init];
}
- (IBAction)addChildViewTapped:(id)sender
{
int numChildViews = [self.listChildViews count];
++numChildViews;
// add new child view
NSString *labelForNewView = [NSString stringWithFormat:#"view %d", numChildViews];
CGFloat labelHeight = 28.0;
UILabel *childView = [[UILabel alloc] initWithFrame:CGRectMake(10, numChildViews*labelHeight, 120.0, labelHeight)];
childView.text = labelForNewView;
[self.listChildViews addObject:childView];
[self.view addSubview:childView];
}
#end
Here is code implementation of pauls answer:
- (IBAction)userTouchedButton:(id)sender{
for (int i = 0; i < 100; i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[view setBackgroundColor:[UIColor redColor]];//to distinguish theseViews from other views
[view setTag:i];//to identified it later
[_array insertObject:view atIndex:i];// globleMutble array
[self.view addSubview:view];
}
}
You need not add your views in the .h file. Just instantiate before and where you add them
-(void) addButton
{
UIView *view = [self view];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:#"My Button" forState:UIControlStateNormal];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 0, 0)];
[myLabel setText:#"My Label"];
[button1 sizeToFit];
[myLabel sizeToFit];
[view addSubview:button1];
[view addSubview:myLabel];
}
i want design my navigationBar like following figure, how can i achieve this.is it possible on navigationBar or i don't need navigationBar.
Topics & settings must be buttons, because i want to navigate to another viewController
I don't know whether it is possible on navigation bar.
I saw a similar one using tabbar: Center Button in Tab Bar for iOS
And you can achieve the same thing using : ALToolbar
Also check KLHorizontalselect
Create custom Navigation Bar with custom Delegate for that.. i used this see example bellow..
create CustomNavBar.h file like bellow...
// Created by Paras on 03/12/11.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CustomNavBarDelegate.h"
#interface CustomNavBar : UIView {
NSObject<CustomNavBarDelegate> *delegate;
NSString *strCatOrLoc;
UIButton *btnBack;
UIButton *btnLeft;
UIImageView *imgRightImage;
UILabel *lbl;
UIImageView *imgTitle;
}
#property (nonatomic, retain) UIButton *btnBack;
#property (nonatomic, assign) NSObject<CustomNavBarDelegate> *delegate;
-(void)onclickBack:(id)sender;
- (id) initWithFrame: (CGRect)rect;
-(void)setImage:(UIImage*)img NavTitle:(NSString *)title;
-(void)setTitleImage:(UIImage*)img rightImage:(UIImage *)imgRight;
-(void)setwithoutlogo:(UIImage*)img NavTitle:(NSString *)title;
#end
now see the code of CustomNavBar.m file bellow...
// Created by Paras on 03/12/11.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "CustomNavBar.h"
#import <QuartzCore/QuartzCore.h>
#import "AppDelegate.h"
#implementation CustomNavBar
#synthesize delegate;
#synthesize btnBack;
#pragma mark -
#pragma mark init methods
- (id) initWithFrame: (CGRect)rect {
if (self == [super initWithFrame:rect]) {
//[self setBackgroundColor:[UIColor colorWithRed:241.0f/255.0f green:241.0f/255.0f blue:237.0f/255.0f alpha:1.0f]];
// [self setBackgroundColor:[UIColor whiteColor]];
btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(10.0, 8.0, 50.0, 28.0);
[btnBack addTarget:self
action:#selector(onclickBack:)
forControlEvents:UIControlEventTouchDown];
btnBack.layer.masksToBounds = YES;
btnBack.layer.cornerRadius = 8.0;
btnBack.layer.borderWidth = 0.5;
btnBack.layer.borderColor = [[UIColor blackColor] CGColor];
//btnBack.titleLabel.textColor = [UIColor blackColor];
// btnBack.titleLabel.text = #"Back";
[btnBack.titleLabel setFont:Arial13];
[btnBack setTitle:#"" forState:UIControlStateNormal];
[btnBack setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// [btnBack setBackgroundColor:[UIColor colorWithRed:241.0f/255.0f green:241.0f/255.0f blue:237.0f/255.0f alpha:1.0f]];
[btnBack setBackgroundColor:[UIColor clearColor]];
[self addSubview:btnBack];
btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
btnLeft.frame = CGRectMake(280.0, 8.0, 310.0, 28.0);
[btnLeft addTarget:self
action:#selector(onclickLeft:)
forControlEvents:UIControlEventTouchDown];
[btnLeft setBackgroundColor:[UIColor clearColor]];
[self addSubview:btnLeft];
}
return self;
}
-(void)setImage:(UIImage*)img NavTitle:(NSString *)title {
imgRightImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,44.0)];
imgRightImage.contentMode = UIViewContentModeScaleAspectFill;
imgRightImage.clipsToBounds = YES;
imgRightImage.layer.masksToBounds = YES;
// imgRightImage.layer.cornerRadius = 11.0;
// imgRightImage.layer.borderWidth = 0.5;
[imgRightImage setImage:img];
[imgRightImage setBackgroundColor:[UIColor clearColor]];
[self addSubview:imgRightImage];
[imgRightImage release];
}
-(void)setwithoutlogo:(UIImage*)img NavTitle:(NSString *)title {
imgRightImage = [[UIImageView alloc] initWithFrame:CGRectMake(275.0,2.0,40.0,40.0)];
imgRightImage.contentMode = UIViewContentModeScaleAspectFill;
imgRightImage.clipsToBounds = YES;
imgRightImage.layer.masksToBounds = YES;
// imgRightImage.layer.cornerRadius = 11.0;
// imgRightImage.layer.borderWidth = 0.5;
//[imgRightImage setImage:img];
[imgRightImage setBackgroundColor:[UIColor clearColor]];
[self addSubview:imgRightImage];
[imgRightImage release];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(70.0, 7.0, 180, 30.0)];
// lbl.font = [UIFont fontWithName:#"Arial" size:20.0];
lbl.font = Arial16;
lbl.numberOfLines = 1;
lbl.tag = 11;
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor blackColor];
lbl.textAlignment = UITextAlignmentCenter;
lbl.text = title;
[self addSubview:lbl];
}
-(void)setTitleImage:(UIImage*)img rightImage:(UIImage *)imgRight {
imgRightImage = [[UIImageView alloc] initWithFrame:CGRectMake(275.0,2.0,40.0,40.0)];
imgRightImage.contentMode = UIViewContentModeScaleAspectFill;
imgRightImage.clipsToBounds = YES;
imgRightImage.layer.masksToBounds = YES;
[imgRightImage setImage:imgRight];
[imgRightImage setBackgroundColor:[UIColor clearColor]];
[self addSubview:imgRightImage];
[imgRightImage release];
imgTitle = [[UIImageView alloc] initWithFrame:CGRectMake(68.0,3.0,200.0,38.0)];
imgTitle.contentMode = UIViewContentModeScaleToFill;
[imgTitle setBackgroundColor:[UIColor clearColor]];
[imgTitle setImage:img];
[self addSubview:imgTitle];
[imgTitle release];
}
-(void)onclickLeft:(id)sender{
NSLog(#">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Nav LeftClick");
[delegate btnleft_clicked:self];
}
-(void)onclickBack:(id)sender {
NSLog(#">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Nav BackClick");
[delegate popViewController:self];
}
#pragma mark -
#pragma mark
- (void)dealloc {
//[array release];
[super dealloc];
}
#end
and after that create Delegate class file like bellow..
#class CustomNavBar;
#protocol CustomNavBarDelegate
#required
- (void)popViewController:(CustomNavBar *)navBar;
-(void)btnleft_clicked:(CustomNavBar *)navBar1;
#end
after use this code in your classes ..For Example..
in .h file import it and then use like bellow..
#import "CustomNavBarDelegate.h"
#class CustomNavBar;
#interface ViewController : UIViewController<CustomNavBarDelegate>
{
CustomNavBar *navBar;
}
- (void)popViewController:(CustomNavBar *)navBar1;
#end
and in .m file define that delegate method and create and add navigation like bellow...
- (void)viewDidLoad
{
navBar = [[CustomNavBar alloc] initWithFrame:CGRectMake(0, 0, 322, 44)];
[navBar setDelegate:self];
[self.view addSubview:navBar];
[navBar setImage:[UIImage imageNamed:#"yourImageName"] NavTitle:#"yourTitle"];
}
- (void)popViewController:(CustomNavBar *)navBar1 {
// NSLog(#">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> delegate called");
[self.navigationController popViewControllerAnimated:YES];
}
-(void)btnleft_clicked:(CustomNavBar *)navBar1{
NSLog(#"\n\n btn Left Clicked InviteFriendsView");
}
NOTE: This is just an example , here implement your logic with your requirement.
Here you can also add 3rd button in middle and also define the method for called it in Delegate and also in this another .m file..
i hope this helpful for you...
i did this ,see below code
//IvisionApps Button
UIButton *ivisionButton= [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *ivisionButtonImage = [UIImage imageNamed:#"ivisionapps"];
UIImage *ivisionButtonImagePressed = [UIImage imageNamed:#"ivisionappsSelected"];
[ivisionButton setBackgroundImage:ivisionButtonImage forState:UIControlStateNormal];
[ivisionButton setBackgroundImage:ivisionButtonImagePressed forState:UIControlStateHighlighted];
[ivisionButton addTarget:self action:#selector(goIVisionApp) forControlEvents:UIControlEventTouchUpInside];
ivisionButton.frame = CGRectMake(-8, -20, 106, 38);
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(-8, -20, 106, 38)];
backButtonView.bounds = CGRectOffset(backButtonView.bounds, -14, -7);
[backButtonView addSubview:ivisionButton];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
self.navigationItem.rightBarButtonItem = backBarButton;
//Auxilaries Button
UIButton *auxiliariesButton= [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *auxiliariesButtonImage = [UIImage imageNamed:#"Auxiliaries"];
UIImage *auxiliariesButtonImagePressed = [UIImage imageNamed:#"AuxiliariesSelected"];
[auxiliariesButton setBackgroundImage:auxiliariesButtonImage forState:UIControlStateNormal];
[auxiliariesButton setBackgroundImage:auxiliariesButtonImagePressed forState:UIControlStateHighlighted];
[auxiliariesButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
auxiliariesButton.frame = CGRectMake(-19, -20, 106, 38);
UIView *auxiliariesButtonView = [[UIView alloc] initWithFrame:CGRectMake(-19, -20, 106, 38)];
auxiliariesButtonView.bounds = CGRectOffset(auxiliariesButtonView.bounds, -14, -7);
[auxiliariesButtonView addSubview:auxiliariesButton];
UIBarButtonItem *auxiliariesBarButton = [[UIBarButtonItem alloc] initWithCustomView:auxiliariesButtonView];
self.navigationItem.leftBarButtonItem = auxiliariesBarButton;
//hide backBarButton of NavigationItem
[self.navigationItem setHidesBackButton:YES];
I have made a simple subclass of UIView which has a few subviews but its breaking for a reason I can't see.
In my storyboard view controller I have added a UIView and made it the shape I want it, and added my custom class to it. The contents of my subclass is below:
#implementation PersonDetailCell
#synthesize button, requiredMarker, textField;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)layoutSubviews
{
[self setBackgroundColor:[UIColor colorWithRed:87.0f/255.0f green:153.0f/255.0f blue:191.0f/255.0f alpha:1.0f]];
textField = [[DetailTextField alloc] initWithFrame:CGRectMake(10, 0, self.frame.size.width -20, self.frame.size.height)];
[textField setFont:[UIFont fontWithName:#"Helvetica" size:14.0f]];
[textField setTextColor:[UIColor whiteColor]];
[textField setHidden:YES];
[self addSubview:textField];
button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setHidden:YES];
[self addSubview:button];
requiredMarker = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 4, 22)];
[requiredMarker setBackgroundColor:[UIColor redColor]];
[requiredMarker setHidden:YES];
[self addSubview:requiredMarker];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
Then in my view controller code I have imported my subclass and hooked up an IBOutlet of it to my view in my storyboard. In my viewDidLoad of my view controller I also try setting background colour of this view but nothing happens.
All of my code runs without crashing, but my issues:
I can't set the background colour from my view controller, (or any other properties for that matter).
When I run the app, the height is more than that I set in my storyboard (width is fine), but I change the height of the view nowhere.
Any ideas? Am i using the wrong approach somehow?
Thanks.
If it is a table cell, then the class should be extending UITableViewCell. Buttons and textfields can be added on the storyboard - then you can use struts and springs (Size Inspector pane) to ensure the view resizes properly. I don't think layoutSubviews should be used for adding new UI elements (maybe for resizing if you need to do it manually).
Here is some working code:
#interface ChildView()
#property (strong, nonatomic) UITextField *textField;
#property (strong, nonatomic) UIButton *button;
#property (strong, nonatomic) UIImageView *requiredMarker;
#end
#implementation ChildView
#synthesize textField= _textField;
#synthesize button = _button;
#synthesize requiredMarker = _requiredMarker;
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self setBackgroundColor:[UIColor colorWithRed:87.0f/255.0f green:153.0f/255.0f blue:191.0f/255.0f alpha:1.0f]];
self.textField = [[UITextField alloc] init];
[self.textField setFont:[UIFont fontWithName:#"Helvetica" size:14.0f]];
[self.textField setTextColor:[UIColor whiteColor]];
self.textField.text = #"Test text";
[self addSubview:self.textField];
self.button = [[UIButton alloc] init];
self.button.backgroundColor = [UIColor greenColor];
[self addSubview:self.button];
self.requiredMarker = [[UIImageView alloc] init];
[self.requiredMarker setBackgroundColor:[UIColor redColor]];
[self addSubview:self.requiredMarker];
}
return self;
}
-(void)layoutSubviews
{
CGSize hostSize = self.frame.size;
float length = 22;
self.textField.frame = CGRectMake(10, length, hostSize.width, hostSize.height-length);
self.button.frame = CGRectMake(0, 0, hostSize.width, length);
self.requiredMarker.frame = CGRectMake(0, 0, 4, length);
}
#end
I am slowly developing a custom button (while learning how to implement classes etc).
I have a ViewController, which imports a SuperButton.h (UIControl) and then creates an instance of the SuperButton. (This works, as proved by a NSLog.)
But I cannot get the method in SuperButton to display a Label.
I think this might have something to with the '.center' value or the 'addSubview' command?
I would really appreciate your help. Thanks.
Here is my SuperButton.m code:
#import "SuperButton.h"
#implementation SuperButton
#synthesize firstTitle;
#synthesize myLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void) shoutName{
NSLog(#"My name is %#", firstTitle);
self.backgroundColor = [UIColor blueColor];
CGRect labelFrame = CGRectMake(0.0f, 0.0f, 100.0f, 50.0f);
self.myLabel = [[UILabel alloc] initWithFrame:labelFrame];
self.myLabel.text = #"Come on, don't be shy.";
self.myLabel.font = [UIFont italicSystemFontOfSize:14.0f];
self.myLabel.textColor = [UIColor grayColor];
self.myLabel.center = self.center;
[self addSubview:self.myLabel];
}
Here is the code in my ViewController:
- (void) makeButton{
SuperButton *button1 = [[SuperButton alloc] init];
button1.firstTitle = #"Mr. Ploppy";
[button1 shoutName];
}
(EDIT:) Just in case, here's the SuperButton.h code:
#import <UIKit/UIKit.h>
#interface SuperButton : UIControl
#property (nonatomic, strong) NSString *firstTitle;
#property (nonatomic, strong) UILabel *myLabel;
- (void) shoutName;
#end
I found an answer elsewhere.
I needed to addSubview the 'button'. My working code now looks like this:
- (void) makeButton{
SuperButton *button1 = [[SuperButton alloc] init];
button1.firstTitle = #"Mr. Ploppy";
[button1 shoutName];
[self.view addSubview:button1.myLabel];
[self.view sendSubviewToBack:button1.myLabel];
}
You are initializing your button not with initWithFrame: method, but with simple init. This makes the button of CGRectZero size. Change this line:
SuperButton *button1 = [[SuperButton alloc] init];
to this:
SuperButton *button1 = [[SuperButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 50.0f)];
or add a call to setFrame: after initializing your button.
In my project i'm using a UITextField which is embedded into a plain white UIButton with rounded corners in order to create a "beautiful" text field. This code is used several times in different views so I decided to create a custom UIView for this purpose. The code for this custom UIView:
BSCustomTextField.h
#import <Foundation/Foundation.h>
#interface BSCustomTextField : UIView {
UIButton* btnTextFieldContainer;
UITextField* textField;
NSString* text;
}
#property (retain, nonatomic) UIButton* btnTextFieldContainer;
#property (retain, nonatomic) UITextField* textField;
#property (retain, nonatomic) NSString* text;
-(id)initWithPosition:(CGPoint)position;
#end
BSCustomTextField.m
#import "BSCustomTextField.h"
#import <QuartzCore/QuartzCore.h>
#implementation BSCustomTextField
#synthesize btnTextFieldContainer, textField, text;
-(id)initWithPosition:(CGPoint)position{
if (self == [super init]) {
btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(position.x, position.y, 260, 50)];
btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
[[btnTextFieldContainer layer] setCornerRadius:3.];
[[btnTextFieldContainer layer] setMasksToBounds:YES];
[[btnTextFieldContainer layer] setBorderWidth:0.];
textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];
textField.backgroundColor = [UIColor whiteColor];
textField.clearButtonMode = UITextFieldViewModeAlways;
textField.returnKeyType = UIReturnKeySend;
textField.keyboardType = UIKeyboardTypeEmailAddress;
textField.font = [UIFont fontWithName:#"Helvetica-Bold" size:20.];
[btnTextFieldContainer addSubview:textField];
[self addSubview:btnTextFieldContainer];
}
return self;
}
-(void)dealloc{
[btnTextFieldContainer release];
[textField release];
[super dealloc];
}
#end
So, when using this view in the viewDidLoad of the container view (see code below) the view is properly rendered on the desired position and looks exactly as specified but it does not react on touch events and thus does not become first responder when touched.
Code:
searchTextField = [[BSCustomTextField alloc] initWithPosition:CGPointMake(30, 150)];
searchTextField.textField.placeholder = NSLocalizedString(#"lsUserName", #"");
[[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:searchTextField];
Calling [searchTextField.textField becomeFirstResponder] programmatically works properly and makes the keyboard to appear.
But, the interesting part is, if I embed the code of BSCustomTextField inline in my container just like this:
UIButton* btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(30, 150, 260, 50)];
btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
[[btnTextFieldContainer layer] setCornerRadius:3.];
[[btnTextFieldContainer layer] setMasksToBounds:YES];
[[btnTextFieldContainer layer] setBorderWidth:0.];
searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];
searchTextField.backgroundColor = [UIColor whiteColor];
searchTextField.clearButtonMode = UITextFieldViewModeAlways;
searchTextField.returnKeyType = UIReturnKeySend;
searchTextField.keyboardType = UIKeyboardTypeEmailAddress;
searchTextField.font = [UIFont fontWithName:#"Helvetica-Bold" size:20.];
searchTextField.placeholder = NSLocalizedString(#"lsUserName", #"");
[btnTextFieldContainer addSubview:searchTextField];
[[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:btnTextFieldContainer];
[btnTextFieldContainer release];
everything works as expected and the textfield reacts on touches. The type of the searchTextField is properly set in the header file for each case. The only difference is that in the first case I have an additional wrapping UIView on the UIButton and the UITextFiled. I have no idea what to do to make the text field to become first responder on touch.
Thanks a lot in advance,
Roman
Ok, I've got the solution. raaz's point to the UIResponder class tipped me off to the idea that there must be something wrong in the responder chain, thus I did a little research and found this topic: Allowing interaction with a UIView under another UIView in which exact the same problem is discussed.
Here is the new working code for BSCustomTextField.m
#import "BSCustomTextField.h"
#import <QuartzCore/QuartzCore.h>
#implementation BSCustomTextField
#synthesize btnTextFieldContainer, textField, text;
-(id)initWithPosition:(CGPoint)position{
if (self == [super init]) {
btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(position.x, position.y, 260, 50)];
btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
[[btnTextFieldContainer layer] setCornerRadius:3.];
[[btnTextFieldContainer layer] setMasksToBounds:YES];
[[btnTextFieldContainer layer] setBorderWidth:0.];
textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];
textField.backgroundColor = [UIColor whiteColor];
textField.clearButtonMode = UITextFieldViewModeAlways;
textField.returnKeyType = UIReturnKeySend;
textField.keyboardType = UIKeyboardTypeEmailAddress;
textField.font = [UIFont fontWithName:#"Helvetica-Bold" size:20.];
[btnTextFieldContainer addSubview:textField];
[self addSubview:btnTextFieldContainer];
}
return self;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
// UIView will be "transparent" for touch events if we return NO
return YES;
}
-(void)dealloc{
[btnTextFieldContainer release];
[textField release];
[super dealloc];
}
#end
Since the clickable area fills out the entire BSCustomTextField view we can simply return YES in pointInside:withEvent:
Thank you all for pointing me into the right direction.
Refer UIResponder class
This class has instance method becomeFirstResponder: & isFirstResponder:
Also do not forget to set textfield editing property to YES
Oh, you're completely wrong with doing this way. You should add stretchable image into UIImageView and put it below UITextField.
UIImage *backgroundImage = [[UIImage imageNamed:#"fieldBackground.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
Or you can try to disable userInteraction in UIButton.