Border around UITextView is not showing up - iphone

I am trying to add border around UITextView by using
self.textView.layer.borderWidth = 1;
self.textView.layer.borderColor = [[UIColor brownColor] CGColor];
but it is not showing up. If anyone can help me with why it is not showing up. Eventhough i have added Quartz framework. But still is not showing up.
#import "uitextviewViewController.h"
#import <QuartzCore/QuartzCore.h>
#implementation uitextviewViewController
#synthesize textView;
#synthesize navBar;
- (void)dealloc {
[navBar release];
[textView release];
[super dealloc];
}
- (void) viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem * button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(done:)];
[[self navigationItem] setRightBarButtonItem:button];
[button release];
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease];
self.textView.textColor = [UIColor whiteColor];
self.textView.font = [UIFont fontWithName:#"Arial" size:15];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor brownColor];
self.textView.layer.borderWidth = 1;
self.textView.layer.borderColor = [[UIColor brownColor] CGColor];
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"This is UITextView\n\nThis is UITextView\n\nThis is UITextView\n\nThis is UITextView\n\nThis is UITextView.";
self.textView.scrollEnabled = YES;
[self.view addSubview: self.textView];
}
I will appreciate help.
Thanks in advance.

Following code is use for the give the arc of the UITextbox or other controller for that you need to use the #import frame work then you can use the following code for the border or arc shape on the control.
imgThumb.layer.cornerRadius = 5;
imgThumb.layer.masksToBounds = YES;
imgThumb.layer.borderColor = [UIColor grayColor].CGColor;
imgThumb.layer.borderWidth = 0.9;
If any query regarding this please comment here...
Happy coding

Here, use this.
Modify it as you'd like.
UITextFieldWrapper.h
#import <UIKit/UIKit.h>
#interface UITextFieldWrapper : UIView
#property (nonatomic, weak, readonly) UITextField * textField;
#end
UITextFieldWrapper.m
#import "UITextFieldWrapper.h"
#import <QuartzCore/QuartzCore.h>
#implementation UITextFieldWrapper
#synthesize textField = _textField;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.layer.borderWidth = 1.0;
self.layer.borderColor = [[UIColor blackColor] CGColor];
self.backgroundColor = [UIColor whiteColor];
self.clipsToBounds = YES;
UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(3, 3, frame.size.width - 6, 21)];
textField.backgroundColor = [UIColor whiteColor];
[self addSubview:textField];
_textField = textField;
}
return self;
}
#end

Related

Label not showing up in overlay UIView

Ok, I have done this kind of thing a million times but this particular case escapes me and I'm sure its just something stupid I am missing...
But I am trying to display a UIView on top of a mapView to give the impression the mapview is 'disabled.'
I created a UIView subclass and added a single label. like so...
.h
#import <UIKit/UIKit.h>
#interface DisabledOverlayView : UIView
#property (nonatomic, strong) UILabel *disabledOverlayViewText;
#end
nothing complicated...
in my .m I set the text and add it to my UIView.
#synthesize disabledOverlayViewText = _disabledOverlayViewText;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.alpha = 1;
_disabledOverlayViewText.textColor = [UIColor whiteColor];
_disabledOverlayViewText.frame = CGRectMake(0, 0, 200, 200);
// [_disabledOverlayViewText setCenter:self.center];
_disabledOverlayViewText.text = #"testing";
[self addSubview:_disabledOverlayViewText];
}
return self;
}
then the class i wish to use this overlay i call it with...
DisabledOverlayView *disabledView = [[DisabledOverlayView alloc] initWithFrame:CGRectMake(12, 12, _mapView.frame.size.width, _mapView.frame.size.height)];
disabledView.disabledOverlayViewText.text = #"No Location";
[self.view addSubview:disabledView];
None of this is too complicated.. but the label doesn't show up. the overlay appears just fine, but no label text. Ive messed with the frames of the label thinking maybe it was off the visible view but no dice. and i cannot figure out what is going on.
Replace your initWithFrame method with the below code. I have added the alloc init line and changes the background color of the label. Try it and see whether it works :)
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.alpha = 1;
_disabledOverlayViewText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
_disabledOverlayViewText.textColor = [UIColor whiteColor];
_disabledOverlayViewText.backgroundColor = [UIColor blackColor];
// [_disabledOverlayViewText setCenter:self.center];
_disabledOverlayViewText.text = #"testing";
[self.view addSubview:_disabledOverlayViewText];
}
It seems the label is not allocated and it's nil.
Try this
(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.alpha = 1;
self.disabledOverlayViewText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
_disabledOverlayViewText.textColor = [UIColor whiteColor];
// [_disabledOverlayViewText setCenter:self.center];
_disabledOverlayViewText.text = #"testing";
[self addSubview:_disabledOverlayViewText];
}

Make UINavigationBar title be user editable

How do I make UINavigationBar title to be user editable, I've tried setting the titleView to be a textfield however it doesn't look the same.. It's missing that outline or drop shadow. I'm aiming for it to look like the default one.
This is what i'm implementing at the moment:
_titleField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 26)];
[_titleField setDelegate:self];
_titleField.text = _bugDoc.data.title;
_titleField.font = [UIFont boldSystemFontOfSize:20];
_titleField.textColor = [UIColor whiteColor];
_titleField.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = _titleField;
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadTitle];
}
- (void)loadTitle
{
txtField_navTitle = [[UITextField alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x,7,self.view.bounds.size.width,31)];
txtField_navTitle.delegate = self;
[txtField_navTitle setBackgroundColor:[UIColor clearColor]];
txtField_navTitle.textColor = [UIColor whiteColor];
txtField_navTitle.textAlignment = UITextAlignmentCenter;
txtField_navTitle.borderStyle = UITextBorderStyleNone;
txtField_navTitle.font = [UIFont boldSystemFontOfSize:20];
txtField_navTitle.autocorrectionType = UITextAutocorrectionTypeNo;
txtField_navTitle.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.navigationItem setTitleView:txtField_navTitle];
txtField_navTitle.layer.masksToBounds = NO;
txtField_navTitle.layer.shadowColor = [UIColor whiteColor].CGColor;
txtField_navTitle.layer.shadowOpacity = 0;
[txtField_navTitle release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
self.title = textField.text;
return YES;
}
Please dont forget to #import <QuartzCore/QuartzCore.h>
Try this way this works for me.
// Custom Navigation Title.
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
tlabel.text=self.title;
tlabel.textAlignment=NSTextAlignmentCenter;
tlabel.font = [UIFont boldSystemFontOfSize:17.0];
tlabel.textColor=[UIColor colorWithRed:7.0/255.0 green:26.0/255.0 blue:66.0/255.0 alpha:1.0];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=tlabel;

how do i achieve this on navigationBar

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];

Creating a Label via a method in a class

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.

UITextField embedded in an UIView does not respond to touch events

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.