UITextField as sublayer - iphone

I am adding a UITextField's layer as a sublayer to an existing layer that is actually a UIImageView. I apply perspective rotation on the image view and it works fine. But the UITextField does not become active. The beginFirstResponder message to it does not show the keyboard. Tapping it doesn't work, it stays inactive from user interaction. I also tried setting the zPosition for the textfield's layer so that it is above the imageview layer and it's zPosition.
rightband = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"right-band.png"]];
rightband.layer.frame = CGRectMake(623.5, 310.5, 624, 210);
rightband.layer.zPosition = 40.0f;
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 260, 26)];
usernameField.clearButtonMode = UITextFieldViewModeWhileEditing;
usernameField.keyboardType = UIKeyboardTypeDefault;
usernameField.autocapitalizationType = UITextAutocapitalizationTypeNone;
usernameField.borderStyle = UITextBorderStyleNone;
usernameField.backgroundColor = [UIColor clearColor];
usernameField.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
usernameField.textColor = UIColorFromRGB(0x303030);
usernameField.textAlignment = UITextAlignmentLeft;
usernameField.placeholder = #"Username";
usernameField.autocorrectionType = UITextAutocorrectionTypeNo;
usernameField.returnKeyType = UIReturnKeyNext;
usernameField.delegate = self;
usernameField.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
usernameField.layer.zPosition = 100.0f;
usernameField.layer.frame = CGRectMake(10, 10, 260, 26);
[rightband.layer addSublayer:usernameField.layer];

You should add the text field view in order for it to respond to touches.
So do :
[rightband addSubview:usernameField];

Can you try to change the view heirarchy to the following
UIView -> UIImageView and UITextField
Dont add the UITextField as a child to UIImageView
The UITextField and UIImageView will be both child's to a UIView

Related

UIButton not working. Not first responder or is hiding behind view

My UIButton doesn't respond. I think it is behind the UIView or that it is not the first responder. I made a custom view which subclass is MKAnnotationView. Maybe this is a part of the problem. I also tried to use userInteractionEnabled, but no luck. Anyone had the same issue?
- (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
// Create selected and un-selected pin images
_normalPin = [UIImage imageNamed:#"map_pin.png"];
_selectedPin = [UIImage imageNamed:#"selected_map_pin.png"];
// Set current pin to unselected image
self.image = _normalPin;
// cast annotation an our expected BLAnnotation
Annotation *blAnnotation = (Annotation *)annotation;
// create title label and size to text
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 280, 20)];
_titleLabel.text = blAnnotation.title;
_titleLabel.textAlignment = UITextAlignmentLeft;
_titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:13];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.7];
_titleLabel.shadowOffset = CGSizeMake(0, -1.0);
_titleLabel.backgroundColor = [UIColor clearColor];
_titleLabel.lineBreakMode = UILineBreakModeWordWrap;
_titleLabel.numberOfLines = 3;
CGSize titleLabelSize = [blAnnotation.title sizeWithFont: _titleLabel.font constrainedToSize: CGSizeMake(280, 600) lineBreakMode: _titleLabel.lineBreakMode];
_titleLabel.frame = CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y, 280, titleLabelSize.height);
// create subtitle label and size to text
_subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _titleLabel.frame.size.height + 8, 280, 20)];
_subtitleLabel.text = blAnnotation.subtitle;
_subtitleLabel.textAlignment = UITextAlignmentLeft;
_subtitleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:11];
_subtitleLabel.textColor = [UIColor whiteColor];
_subtitleLabel.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.7];
_subtitleLabel.shadowOffset = CGSizeMake(0, -1.0);
_subtitleLabel.backgroundColor = [UIColor clearColor];
_subtitleLabel.lineBreakMode = UILineBreakModeWordWrap;
_subtitleLabel.numberOfLines = 4;
CGSize subtitleLabelSize = [blAnnotation.subtitle sizeWithFont: _subtitleLabel.font constrainedToSize: CGSizeMake(235, 600) lineBreakMode: _subtitleLabel.lineBreakMode];
_subtitleLabel.frame = CGRectMake(_subtitleLabel.frame.origin.x, _subtitleLabel.frame.origin.y, subtitleLabelSize.width, subtitleLabelSize.height);
// create custom button
_infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
_infoButton.frame = CGRectMake(0, _titleLabel.frame.size.height + _subtitleLabel.frame.size.height + 80, 175, 50);
//_infoButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//_infoButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//[_infoButton setImage:[UIImage imageNamed:#"more_info.png"] forState:UIControlStateNormal];
[_infoButton addTarget:self action:#selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
// create callout view to be shown once a annotation view is selected
_calloutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, _titleLabel.frame.size.height + _subtitleLabel.frame.size.height + _infoButton.frame.size.height + 5)];
_calloutView.hidden = YES;
_calloutView.userInteractionEnabled = YES;
[self addSubview:_calloutView];
// create rounded rect background and size to text
UIImageView *backgroundRect = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, _calloutView.frame.size.width, _calloutView.frame.size.height)];
//[backgroundRect setAlpha:0.8];
//[backgroundRect setOpaque:NO];
[backgroundRect.image drawInRect:_calloutView.frame];
UIGraphicsBeginImageContext(backgroundRect.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect frame = _calloutView.bounds;
[[UIColor colorWithRed:.2 green:.2 blue:.2 alpha:1] set];
CGPathRef roundedRectPath = [self roundedRectPath:frame radius:5];
CGContextAddPath(ctx, roundedRectPath);
CGContextFillPath(ctx);
CGPathRelease(roundedRectPath);
backgroundRect.userInteractionEnabled = YES;
backgroundRect.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[_calloutView addSubview:backgroundRect];
[_calloutView addSubview:_titleLabel];
[_calloutView addSubview:_subtitleLabel];
[_calloutView addSubview:_infoButton];
// position callout above pin
_calloutView.frame = CGRectMake(-140, -_calloutView.frame.size.height+10, _calloutView.frame.size.width, _calloutView.frame.size.height);
backgroundRect = nil;
return self;
}
My guess is that your button is pointing to itself as the target. If the button is not the first responder and it is tapped, its target function is not called in the above scenario. I fixed my similar issue by using lazy var to declare the UIButton in Swift 4 Xcode 9, pointing the target to the parent view / view controller.
// Sets target to parent view or view controller
// someFunction is called even if the button is not the first responder when tapped
lazy var nextCreateButton: UIButton = {
let button = UIButton(type: .system)
button.addTarget(self, action: #selector(someFunction), for: .touchUpInside)
return button
}()
// Sets target to the button itself
// someFunction is not called when the button is not the first responder
let nextCreateButton: UIButton = {
let button = UIButton(type: .system)
button.addTarget(self, action: #selector(someFunction), for: .touchUpInside)
return button
}()
Try using [self addSubview:_calloutView] after you add your button and your lables to *_calloutView* as subviews, not before.
Let me know how it turns out

UItextFiled text on center

I have this textField
who doing text on center?///////////////////////////////////
textPredmet = [[UITextField alloc] initWithFrame:CGRectMake(20, 55, 200, 40)];
textPredmet.borderStyle = UITextBorderStyleNone;
textPredmet.textColor = [UIColor blackColor]; //text color
textPredmet.font = [UIFont systemFontOfSize:15.0]; //font size
[textPredmet drawTextInRect:CGRectMake( 30,55,200,40)];
textPredmet.placeholder = #"Введите текст"; //place holder
textPredmet.backgroundColor = [UIColor clearColor]; //background color
textPredmet.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textPredmet.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
textPredmet.returnKeyType = UIReturnKeyDone; // type of the return key
textPredmet.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textPredmet.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
textPredmet.background = [UIImage imageNamed:#"input.png"];
textPredmet.textAlignment=UITextAlignmentLeft+1;
[self.view addSubview:textPredmet];
Set below line to your code.
textPredmet.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
It will set the text center vertically.
textPredmet.textAlignment = UITextAlignmentCenter;
It will set the text center horizontally.
Also, remove below lines from your code.
[textPredmet drawTextInRect:CGRectMake( 30,55,200,40)];
I guess, UITextAlignmentLeft+1 == UITextAlignmentCenter

ContainerView hide UILabel

I want to use UILabel in ContainerView.
So I am using this code for that.
UILabel *myLabel = [[[UILabel alloc] initWithFrame:CGRectMake(16, 60, 300, 150)] autorelease];
myLabel.numberOfLines = 0;
myLabel.font = [UIFont systemFontOfSize:13.5];
myLabel.text = [theQuiz objectAtIndex:row+3] ;
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.layer.cornerRadius = 8.0;
[myLabel sizeToFit];
[self.view addSubview:myLabel];
//ContainerView
UIView *ChallengeView = [[UIView alloc] initWithFrame:CGRectMake(8, 55, 300, 10 + Challenge.frame.size.height)];
ChallengeView.layer.borderColor = [[UIColor purpleColor ] CGColor];
[ChallengeView setBackgroundColor:[UIColor whiteColor]];
ChallengeView.layer.cornerRadius = 8 ;
ChallengeView.layer.borderWidth = 1.5;
[self.view addSubview:ChallengeView];
[ChallengeView release];
Now problem is that when i set background color for ContainerView it hides the text of myLabel
Any Solution ??
What is happening is that you containerView is being added above label either you add label after containerView or do this:
[self.view bringSubviewToFront:myLabel];
You add the ChallengeView first , Then add the myLabel.
Otherwise u can do as like #xs2bush said,
[self.view bringSubviewToFront:myLabel];
Bcz the ChallengeView hides the label.

UITextField Autoresize in UITableViewCell

I have a UITextField inside of a UITableView cell. When I rotate the device, the textfield is still the same width it was for Portrait mode. I know that I need to use autoresizing to make the field fill the cell when I am in landscape, but I am not sure how to do this, as I have tried and failed.
How would I do this?
cell.textLabel.text = #"Email";
UITextField *field2 = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, width, 30)];
field2.adjustsFontSizeToFitWidth = YES;
field2.textColor = [UIColor blackColor];
field2.keyboardType = UIKeyboardTypeEmailAddress;
field2.returnKeyType = UIReturnKeyNext;
field2.placeholder = #"Required";
field2.tag = 3;
field2.delegate = self;
field2.autoresizingMask = UIViewAutoresizingFlexibleWidth;
field2.backgroundColor = [UIColor blueColor];
// Finish building cell
field2.autocorrectionType = UITextAutocorrectionTypeNo;
field2.autocapitalizationType = UITextAutocapitalizationTypeNone;
field2.textAlignment = UITextAlignmentLeft;
[field2 addTarget:self action:#selector(updateFields:) forControlEvents:UIControlEventEditingChanged];
field2.text = email;
self.emailField = field2;
[cell addSubview:field2];
[field2 release];
Check if you tableView has Auto Resize subivew is set to YES.
Check you are returning YES for all the supported rotations.
(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:
(NSTimeInterval)duration{
}

UITextField Black Pixels at Edges

I've created a UITextField programmatically with the following code:
self._maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, labelWidth, labelHeight)];
self._maxPriceField.borderStyle = UITextBorderStyleRoundedRect;
self._maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;
self._maxPriceField.font = fieldFont;
self._maxPriceField.delegate = self;
The problem I'm having is that my UITextField ends up having these strange black pixels on the edges. This happens both on the device and in the simulator. You can see in the screenshot below:
When I create the same UITextField using IB, with the same specs and background, I have no problem. Unfortunately I need to create this UITextField programmatically.
Has anybody seen this before? What to do?
It seems this is the way textfields are drawn on the screen. I played around with your code a little bit, and if I set the text field height lower than about 20, you start to see a noticeable "shadow" that is clearly not drawn correctly.
My suggestion is to either use a height of 20 or higher for the text field, or to use a different style, such as Bezel or Line and set the background to white.
Here is a screenshot to demonstrate:
And here is the code I used to draw these:
int labelWidth = 100;
int labelHeight = 10;
_maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, labelWidth, labelHeight)];
_maxPriceField.borderStyle = UITextBorderStyleRoundedRect;
_maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;
//_maxPriceField.font = fieldFont;
//_maxPriceField.delegate = self;
[self.view addSubview:_maxPriceField];
UITextField *secondField = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, labelWidth, labelHeight + 10)];
secondField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:secondField];
[secondField release];
UITextField *thirdField = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, labelWidth, labelHeight + 20)];
thirdField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:thirdField];
[thirdField release];
UITextField *fourthField = [[UITextField alloc] initWithFrame:CGRectMake(10, 110, labelWidth, labelHeight + 30)];
fourthField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:fourthField];
[fourthField release];
UITextField *noRoundFirst = [[UITextField alloc] initWithFrame:CGRectMake(10, 160, labelWidth, labelHeight)];
noRoundFirst.borderStyle = UITextBorderStyleBezel;
noRoundFirst.backgroundColor = [UIColor whiteColor];
[self.view addSubview:noRoundFirst];
[noRoundFirst release];
Hope this helps.
Mk