UITextField - Next Button (2) - ios

I have used all the suggestions on the forum and still can not go from one uitextfield to the other by hitting the next button. Am i missing something or have something out of place. Any help would be great. Thanks.
Here is all of my code:
//.h file
#import <UIKit/UIKit.h>
#interface IncomeTransactionViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *businessField;
#property (strong, nonatomic) IBOutlet UITextField *memoField;
#property (strong, nonatomic) IBOutlet UITextField *amountField;
#end
//.m file
#import "IncomeTransactionViewController.h"
#interface IncomeTransactionViewController ()
#end
#implementation IncomeTransactionViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//Business UITextField
self.businessField = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 280, 40)];
self.businessField.returnKeyType = UIReturnKeyNext;
self.businessField.placeholder = #"Business";
self.businessField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.businessField.adjustsFontSizeToFitWidth = TRUE;
self.businessField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.businessField];
//Memo UITextField
self.memoField = [[UITextField alloc] initWithFrame:CGRectMake(20, 350, 280, 40)];
self.memoField.returnKeyType = UIReturnKeyNext;
self.memoField.placeholder = #"Memo";
self.memoField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.memoField.adjustsFontSizeToFitWidth = TRUE;
self.memoField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.memoField];
//Amount UITextField
self.amountField = [[UITextField alloc] initWithFrame:CGRectMake(20, 400, 280, 40)];
self.amountField.returnKeyType = UIReturnKeyDone;
self.amountField.placeholder = #"Amount";
self.amountField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.amountField.adjustsFontSizeToFitWidth = TRUE;
self.amountField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.amountField];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.amountField) {
[textField resignFirstResponder];
} else if (textField == self.businessField) {
[self.businessField resignFirstResponder];
[self.memoField becomeFirstResponder];
} else if (textField == self.memoField) {
[self.memoField resignFirstResponder];
[self.amountField becomeFirstResponder];
}
return YES;
}
#end

You have to take the <UITextFieldDelegate> in your .h file
#interface IncomeTransactionViewController : UIViewController <UITextFieldDelegate>
and in your .m file you need to assign delegate like this
self.businessField = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 280, 40)];
self.businessField.delegate = self; // add this line
self.businessField.returnKeyType = UIReturnKeyNext;
self.businessField.placeholder = #"Business";
self.businessField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.businessField.adjustsFontSizeToFitWidth = TRUE;
self.businessField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.businessField];
//Memo UITextField
self.memoField = [[UITextField alloc] initWithFrame:CGRectMake(20, 350, 280, 40)];
self.memoField.delegate = self; // add this line
self.memoField.returnKeyType = UIReturnKeyNext;
self.memoField.placeholder = #"Memo";
self.memoField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.memoField.adjustsFontSizeToFitWidth = TRUE;
self.memoField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.memoField];
//Amount UITextField
self.amountField = [[UITextField alloc] initWithFrame:CGRectMake(20, 400, 280, 40)];
self.amountField.delegate = self; // add this line
self.amountField.returnKeyType = UIReturnKeyDone;
self.amountField.placeholder = #"Amount";
self.amountField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.amountField.adjustsFontSizeToFitWidth = TRUE;
self.amountField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.amountField];
This should do it. let me know if it still doesn't work

Related

When a custom button is pressed UILabel won't update (Objective-C)

(I'm currently learning iOS Development so I apologize in advanced of my limited knowledge.)
What I am trying to accomplish is that whenever you press either the addition or subtraction button the label will increment by one and will display the result by the label. I am having difficulty with displaying the result in the label.
This is my ViewController.h file:
#import <UIKit/UIKit.h>
#import "StepperView.h"
#interface ViewController : UIViewController{
UILabel *valueLabel;
}
#end
This is my ViewController.m file:
#import "ViewController.h"
#import "StepperView.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
int w;
w = 0;
[super viewDidLoad];
StepperView *stepperView = [[StepperView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
stepperView.center = self.view.center;
[self.view addSubview:stepperView];
// AddButton
UIButton *additionButton = [[UIButton alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
additionButton.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 25, 100, 50);
[self.view addSubview:additionButton];
// Subtraction button
UIButton *subtractionButton = [[UIButton alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
subtractionButton.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 25, 100, 50);
[self.view addSubview:subtractionButton];
// Label
valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
[valueLabel setTextColor:[UIColor redColor]];
[self.view addSubview:valueLabel];
}
#end
This is my Delegate and Protocol:
StepperView.h
#import <UIKit/UIKit.h>
#protocol SwitchViewDelegate
- (void)switchViewValueChanged:(int)number;
#end
#interface StepperView : UIView
{
}
#property (nonatomic,weak) id<SwitchViewDelegate> delegate;
#end
This is my StepperView.m file
#import "StepperView.h"
#implementation StepperView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)setup {
UIView *stepperView = [[UIView alloc] init];
stepperView.frame = CGRectMake(0, 0, 300, 40);
stepperView.backgroundColor = [UIColor colorWithRed:21/255.0 green:101/255.0 blue:192/255.0 alpha:1.0];
[self addSubview:stepperView];
// Add Button
UIButton *rightView = [[UIButton alloc] init];
rightView.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 20, 100, 40);
rightView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[rightView setTitle:#"+" forState:UIControlStateNormal];
[rightView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:rightView];
// Subtract Button
UIButton *leftView = [[UIButton alloc] init];
leftView.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 20, 100, 40);
leftView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[leftView setTitle:#"-" forState:UIControlStateNormal];
[leftView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:leftView];
// Label
UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(stepperView.frame.size.width/2, stepperView.frame.size.height/2, 100, 100)];
[valueLabel setText:#"10"];
[valueLabel setTextColor:[UIColor redColor]];
[self addSubview:valueLabel];
}
- (void)buttonPressed:(id)sender{
UIButton *button = (UIButton *) sender;
NSString *title = button.titleLabel.text;
if([title isEqualToString:#"-"]) {
NSLog(#"MINUS");
} else {
NSLog(#"PLUS");
}
// NSLog(#"%#",title);
}
#end
I am not sure if i understand clear what you want to do, but why don't you try to use the UIStepper button provided by iOS?
You can add an IBAction to your viewController and change the label with the value from the UIStepper.
Using swift:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var valueLabel: UILabel!
var currentValue = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func touchStepper(sender: UIStepper) {
self.valueLabel.text = "\(sender.value)"
}
}
It's not very clear what you're trying you obtain as UI, i'm posting a possible solution which is using
The delegate to pass the information from StepperView to the ViewController
UILabel to present the result which is subview of ViewController's view, instead to be subview of StepperView
ViewController.h:
#import <UIKit/UIKit.h>
#import "StepperView.h"
#interface ViewController : UIViewController
#property (nonatomic, strong) UILabel *valueLabel;
#end
ViewController.m
#import "ViewController.h"
#import "StepperView.h"
#interface ViewController () <StepperViewDelegate>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
StepperView *stepperView = [[StepperView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
stepperView.center = self.view.center;
[self.view addSubview:stepperView];
stepperView.delegate = self;
// Label
self.valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
[self.valueLabel setTextColor:[UIColor redColor]];
[self.view addSubview:self.
valueLabel];
}
#pragma mark - StepperViewDelegate
- (void)stepperView:(StepperView *)stepperView valueChanged:(NSInteger)value {
self.valueLabel.text = [NSString stringWithFormat:#"%d", value];
}
#end
StepperView.h
#import <UIKit/UIKit.h>
#class StepperView
#protocol StepperViewDelegate
- (void)stepperView:(StepperView *)stepperView valueChanged:(NSInteger)value
#end
#interface StepperView : UIView
#property (nonatomic, weak) id<StepperViewDelegate> delegate;
#property (nonatomic, assign) NSInteger stepperValue;
#end
StepperView.m
#import "StepperView.h"
#implementation StepperView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)setup {
UIView *stepperView = [[UIView alloc] init];
stepperView.frame = CGRectMake(0, 0, 300, 40);
stepperView.backgroundColor = [UIColor colorWithRed:21/255.0 green:101/255.0 blue:192/255.0 alpha:1.0];
[self addSubview:stepperView];
// Add Button
UIButton *rightView = [[UIButton alloc] init];
rightView.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 20, 100, 40);
rightView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[rightView setTitle:#"+" forState:UIControlStateNormal];
[rightView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:rightView];
// Subtract Button
UIButton *leftView = [[UIButton alloc] init];
leftView.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 20, 100, 40);
leftView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[leftView setTitle:#"-" forState:UIControlStateNormal];
[leftView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonPressed:(id)sender{
UIButton *button = (UIButton *) sender;
NSString *title = button.titleLabel.text;
if([title isEqualToString:#"-"]) {
self.stepperValue--;
} else {
self.stepperValue++;
}
[self.delegate stepperView:self valueChanged:self.stepperValue];
// NSLog(#"%#",title);
}
#end
This should give you more clear vision how different components should corespondent with each other.

TTTAttributedLabel Delegate didSelectLinkWithURL not getting called

I'm having problem setting up the TTTAttributedLabel within my custom cell. I have already checked many post and still didn't figured how to solve this. (I can see the URL styling at this custom label , but if i click on it nothing happen).
CustomCell.h:
#interface MyCustomCell : UITableViewCell<TTTAttributedLabelDelegate>
#property (nonatomic, strong, readonly) UITapGestureRecognizer *tapRecognizer;
#property (nonatomic, strong, readonly) UILabel *senderLabel;
#property (nonatomic, strong, readonly) UILabel *timeLabel;
#property (nonatomic, strong, readonly) UILabel *dateLabel;
#property (nonatomic, strong) TTTAttributedLabel *customTextLabel;
#end
CustomCell.m:
#implementation MyCustomCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
_dateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_dateLabel.textAlignment = NSTextAlignmentCenter;
_dateLabel.font = [UIFont boldSystemFontOfSize:12.0];
_dateLabel.textColor = [UIColor grayColor];
_dateLabel.text = #"2015-10-10";
_dateLabel.userInteractionEnabled = false;
_senderLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_senderLabel.textAlignment = NSTextAlignmentLeft;
_senderLabel.font = [UIFont boldSystemFontOfSize:14.0];
_senderLabel.textColor = [UIColor whiteColor];
_senderLabel.userInteractionEnabled = false;
_timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_timeLabel.userInteractionEnabled = false;
_timeLabel.textAlignment = NSTextAlignmentCenter;
_timeLabel.font = [UIFont boldSystemFontOfSize:11.0];
_timeLabel.textColor = [UIColor whiteColor];
_customTextLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
_customTextLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
_customTextLabel.delegate = self;
_customTextLabel.backgroundColor = [UIColor clearColor];
_customTextLabel.numberOfLines = 0;
_customTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
_customTextLabel.textColor = [UIColor blackColor];
_customTextLabel.font = [UIFont systemFontOfSize:18.0];
[self.textLabel addSubview:_customTextLabel];
_customTextLabel.userInteractionEnabled = true;
self.imageView.userInteractionEnabled = YES;
self.imageView.layer.cornerRadius = 5.0;
self.imageView.layer.masksToBounds = YES;
}
return self;
}
Thank you for help.
NSString *text = #"Hello this is https://www.google.com";
_customTextLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 100, 300, 20)];
_customTextLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
_customTextLabel.delegate = self;
_customTextLabel.backgroundColor = [UIColor clearColor];
_customTextLabel.numberOfLines = 0;
_customTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
_customTextLabel.textColor = [UIColor blackColor];
_customTextLabel.font = [UIFont systemFontOfSize:18.0];
_customTextLabel.text = text;
[self.view addSubview:_customTextLabel];
And set delegate method of TTTAttributedLabel it works fine please check:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
}

How to delete UITextfield and entered data from it in iOS

I have develop one application and in that I have two UIButton named [+] and [-] and one UITextField default.
Now what I want is when I click on plus [+] button it should add one UITextField in UIView that is work perfectly to me but when I click on minus [-] button it should delete last created UITextField that is not working now for me.
How to manage that?
here is my code to plus[+] button.
- (IBAction)btnPlusClicked:(id)sender
{
scrollViewSpecialitiesTxtFields.hidden = FALSE;
float y;
y = (35 * txtSpecialityFieldCounter);
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, y, 120, 30)];
textField.delegate = self;
textField.borderStyle = UITextBorderStyleRoundedRect;
[arraySpecialitiesTxtFields insertObject:textField atIndex:arraySpecialitiesTxtFields.count];
scrollViewSpecialitiesTxtFields.contentSize = CGSizeMake(125, y+30);
[scrollViewSpecialitiesTxtFields addSubview:textField];
txtSpecialityFieldCounter++;
NSLog(#"txtSpecialityFieldCounter : %d",txtSpecialityFieldCounter);
}
here is my minus [-] button code
-(IBAction)btnMinusClicked:(id)sender
{
[scrollViewSpecialitiesTxtFields.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
txtSpecialityFieldCounter--;
NSLog(#"txtSpecialityFieldCounter : %d",txtSpecialityFieldCounter);
float y;
y = (35 * txtSpecialityFieldCounter);
for(int i = 0 ; i < txtSpecialityFieldCounter; i++)
{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, y, 120, 30)];
textField.delegate = self;
textField.borderStyle = UITextBorderStyleRoundedRect;
[arraySpecialitiesTxtFields insertObject:textField atIndex:arraySpecialitiesTxtFields.count];
scrollViewSpecialitiesTxtFields.contentSize = CGSizeMake(125, y-30);
[scrollViewSpecialitiesTxtFields addSubview:textField];
}
}
I know minus[-] button code is wrong but let me know changes
Create a global variable as NSMutableArray *myAddedTextfields and in viewDidLoad-> myAddedTextFields = [NSMutableArray array];
- (IBAction)plusAction:(id)sender
{
UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 100, 20)];
textField1.text = #"hey how r u";
textField1.backgroundColor = [UIColor grayColor];
textField1.tag = 1;
[self.view addSubview:textField1];
[myAddedTextFields addObject:textField1];
}
- (IBAction)minusAction:(id)sender
{
UITextField *txtField = [myAddedTextFields objectAtIndex:myAddedTextFields.count -1]; //for deleting from the last textfield onwards.
[txtField removeFromSuperView];
[myAddedTextFields removeObjectIdenticalTo:txtField];
}
I have edited your method.. hope this helps.
Whenever you want to add an UITextField on a superview, set the tag of that component (UITextField).
And when you want to remove it from superview then get the reference of that component (UITextField) using the tag and remove it from the superview.
Here is an example that may be helpful to you:
-(IBAction)plusAction:(id)sender
{
UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 100, 20)];
textField1.text = #"hey how r u";
textField1.backgroundColor = [UIColor grayColor];
textField1.tag = 1;
[self.view addSubview:textField1];
}
-(IBAction)minusAction:(id)sender
{
UITextField * textfield = (UITextField *)[self.view viewWithTag:1];
[textfield removeFromSuperview];
}
Yes, you could use the tag option. Or you could use an NSArray holding your UITextFields.
#interface MyTextFieldViewClass ()
#property (nonatomic, retain) NSMutableArray *textFields;
#end
#implementation MyTextFieldViewClass
- (void)viewDidLoad {
[super viewDidLoad];
self.textFields = [NSMutableArray array];
}
- (IBAction)addTextField:(id)sender {
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 100, 20)];
[self.textFields addObject:tf];
[self.view addSubview:tf];
}
- (IBAction)removeTextField:(id)sender {
UITextField *tf = [self.textFields lastObject];
[tf removeFromSuperview];
[self.textFields removeLastObject];
}
#end
This would be a little better since you do not have to iterate over all the views/ids that exist in self.view.subviews. Also, using the tag might not be reliable since you always want to remove the last item, so you would either need to keep the last item number in memory or you need to iterate over all the subviews in order to find the UITextField with the highest tag and in order to remove it.

incompatible pointer types assigning to uilabel from label view

i've been doing a thing here using objective-c, but i'm a beginner in this language, and that's what i did:
I've created two UIView class and named them as LabelView and ButtonView. What i wanted to do is when i touch the button the label text changes. I have given the code below which i did.
I get an error when i touch the button i've created.
Error: "-[LabelView setText:]: unrecognized selector sent to instance 0x7b66cf80"
Warning on the line: self.cslbl_ = lblView; -
"incompatible pointer types assigning to uilabel from label view".
- (void)viewDidLoad {
LabelView *lblView = [[LabelView alloc] init];
[lblView setFrame:CGRectMake(10, 100, 300, 50)];
//lblView.backgroundColor = [UIColor redColor];
self.cslbl_ = lblView;
[self.view addSubview:lblView];
ButtonView *btnView = [[ButtonView alloc] initWithFrame:CGRectMake(110, 200, 100, 50)];
SEL target = #selector(changeText:);
[btnView setSelector:self selector:target];
[self.view addSubview:btnView];
}
-(void) changeText:(UIButton *)btn {
static int k = 0;
if (k%2 == 0)
{
[self.cslbl_ setText:#"Apple is a healthy fruit"];
}
else
{
[self.cslbl_ setText:#"Apple"];
}
NSLog(#"Click value is %d",k);
k++;
}
Custom Label Class
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UILabel *myLbl = [[UILabel alloc] init];
myLbl.frame = CGRectMake(0, 0, 300, 50);
//myLbl.backgroundColor = [UIColor blueColor];
myLbl.text = #"Apple";
self.lbl_ = myLbl;
[self addSubview:myLbl];
}
return self;
}
Custom Button Class
-(id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UIButton *myBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
[myBtn setTitle:#"Touch" forState:UIControlStateNormal];
[myBtn setTitle:#"Touched" forState:UIControlStateHighlighted];
myBtn.backgroundColor = [UIColor grayColor];
self.btn_ = myBtn;
[self addSubview:myBtn];
}
return self;
}
- (void)setSelector:(UIViewController *)vc selector:(SEL)changeText
{
[btn_ addTarget:vc action:changeText forControlEvents:UIControlEventTouchUpInside];
}
Though question is not clear but it seems that your self.cslbl_ is a UILabel and you are assigning lblView to it which of type UIView, thats the reason you are getting warning.
And you are trying to setText to LabelView which is again not a UILabel type that why you are getting crash.
First of all you LabelView should be a subclass of UILabel not UIView but still if its your requirement then you should use below code.
Custom Label Class
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UILabel *myLbl = [[UILabel alloc] init];
myLbl.frame = CGRectMake(0, 0, 300, 50);
[myLb1 setTag:1];
//myLbl.backgroundColor = [UIColor blueColor];
myLbl.text = #"Apple";
self.lbl_ = myLbl;
[self addSubview:myLbl];
}
return self;
}
in viewdidload
LabelView *lblView = [[LabelView alloc] init];
[lblView setFrame:CGRectMake(10, 100, 300, 50)];
self.cslbl_ = (UILabel*)[lblView viewWithTag:1];
[self.view addSubview:lblView];
Now you can set text to your self.cslbl_

View has wrong frame when loaded from xib and shown first time

I created a view to show in MapView annotation callout.
If I didn't use xib and instantiate this view with initWithFrame, then there is no problem.
But if I use xib, size shown on map is bigger, than is should be.
Code in view:
#interface CalloutContentView : UIView
#property (nonatomic) Visit *visit;
+ (id)customView;
#end
#import "CalloutContentView.h"
#interface CalloutContentView ()
#property (weak, nonatomic) IBOutlet UILabel *title;
#property (weak, nonatomic) IBOutlet UILabel *subtitle;
#property (weak, nonatomic) IBOutlet UISegmentedControl *sc;
#end
#implementation CalloutContentView
+ (id)customView
{
CalloutContentView *customView = [[[NSBundle mainBundle] loadNibNamed:#"CalloutContentView" owner:self options:nil] lastObject];
if ([customView isKindOfClass:[CalloutContentView class]])
return customView;
else
return nil;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// _title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, 18)];
_title.font = [UIFont boldSystemFontOfSize:15.0];
// _title.textAlignment = NSTextAlignmentLeft;
_title.textColor = [UIColor colorWithWhite:0.199 alpha:1.000];
_title.numberOfLines = 1;
_title.backgroundColor = [UIColor yellowColor];
[self addSubview:_title];
// _subtitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 25.0, w, 14)];
_subtitle.font = [UIFont systemFontOfSize:12.0];
_subtitle.textAlignment = NSTextAlignmentLeft;
_subtitle.textColor = [UIColor colorWithWhite:0.239 alpha:1.000];
_subtitle.backgroundColor = [UIColor clearColor];
[self addSubview:_subtitle];
// _sc = [[UISegmentedControl alloc]initWithItems:#[#"One",#"Two"]];
_sc.frame = CGRectMake(0, self.frame.size.height - 30, w, 30);
[_sc addTarget:self action:#selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
[self addSubview:_sc];
}
return self;
}
# end
And in MapViewController:
- (void)popupMapCalloutView {
// Change this for creating your Callout View
CalloutContentView *customView = [CalloutContentView customView];
// CalloutContentView *customView = [[CalloutContentView alloc]initWithFrame: CGRectMake(0, 0, 10, 90)];
// if you provide a custom view for the callout content, the title and subtitle will not be displayed
_calloutView.contentView = customView;
VisitAnnotationView *bottomPin = _visitAnnotationViews[_idAnn];
bottomPin.calloutView = _calloutView;
[_calloutView presentCalloutFromRect:bottomPin.bounds
inView:bottomPin
constrainedToView:_mapView
permittedArrowDirections:SMCalloutArrowDirectionAny
animated:YES];
}
That's how it look like when xib is used:

Resources