My app won't allow users to switch keyboards to the number/character keyboard.
I don't have anything in my UI Inspector showing over the bottom of the screen.
I am truly stumped as to what could be causing this issue. Whatever it is blocks the number/symbol switcher, the keyboard language (or Emoji) selector, and the speech to text button. The space bar and return key on the same row continue to operate fine.
The affected UITextField's are subclassed, the keyboard works as expected on the other 5 views in which they are contained, but here is their code:
JCTextField.h
#import <UIKit/UIKit.h>
IB_DESIGNABLE
#interface JCTextField : UITextField
#property (nonatomic, retain) IBInspectable UIColor *underlineColor;
#property (nonatomic) IBInspectable CGFloat underlineWidth;
#property (nonatomic, retain) IBInspectable UIImage *sideImage;
#property (nonatomic) IBInspectable CGFloat internalOffset;
#property (nonatomic) IBInspectable CGFloat borderOffset;
#end
JCTextField.m
#import "JCTextField.h"
#implementation JCTextField
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self != nil) {
self.underlineColor = [UIColor blackColor];
self.underlineWidth = 1;
self.internalOffset = 0;
self.borderOffset = 0;
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CALayer *border = [CALayer layer];
border.borderColor = _underlineColor.CGColor;
border.frame = CGRectMake(_borderOffset, self.frame.size.height - _underlineWidth, self.frame.size.width - (_borderOffset * 2), self.frame.size.height);
border.borderWidth = _underlineWidth;
[self.layer addSublayer:border];
self.layer.masksToBounds = YES;
[self setValue:[UIColor lightTextColor] forKeyPath:#"_placeholderLabel.textColor"];
[self setTextColor:[UIColor whiteColor]];
[self setFont:[UIFont fontWithName:#"Montserrat-Regular" size:16.0]];
if (self.sideImage) {
UIImageView *sideImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width - 30, 0, 30, 30)];
sideImageView.contentMode = UIViewContentModeCenter;
sideImageView.image = self.sideImage;
[self addSubview:sideImageView];
}
[self setTintColor:[UIColor whiteColor]];
[self setValue:[UIColor lightGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
}
- (CGRect)textRectForBounds:(CGRect)bounds {
if (self.sideImage) {
UIEdgeInsets insets = UIEdgeInsetsMake(0, _internalOffset, 0, 18);
return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)];
}
UIEdgeInsets insets = UIEdgeInsetsMake(0, _internalOffset, 0, 0);
return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)];
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
if (self.sideImage) {
UIEdgeInsets insets = UIEdgeInsetsMake(0, _internalOffset, 0, 18);
return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)];
}
UIEdgeInsets insets = UIEdgeInsetsMake(0, _internalOffset, 0, 0);
return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)];
}
#end
Related
My application starts by showing some Intro slides. 4 in total and in the last one, a button is used to close the slides and to go to the main page.
What I wish to make is to remove the button, and to make the last slide slidable. The user continues to slide to the right, and at the end it lands to the main page.
But don't know how to acheave that.
Here is the definition of
#interface IntroSlideViewController ()<ABCIntroViewDelegate>
#property ABCIntroView *introView;
#end
#implementation IntroSlideViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}
-(void)viewDidAppear:(BOOL)animated{
/***************Page Control**************/
self.introView = [[ABCIntroView alloc] initWithFrame:self.view.frame];
self.introView.delegate = self;
self.introView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.000];
[self.view addSubview:self.introView];
/***************************************/
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)onDoneButtonPressed {
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:FIRSTTIME_LOGIN];
InitScreenViewController* initVc = [self.storyboard instantiateViewControllerWithIdentifier:#"initScreenViewController"];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:initVc];
[navVC setNavigationBarHidden:true];
[UIView transitionWithView:[AppDelegate getShareInstance].window duration:0.1f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[AppDelegate getShareInstance].window.rootViewController = navVC;
}completion:nil];
}
#end
And the parent class ABCIntroViewDelegate:
#interface ABCIntroView () <UIScrollViewDelegate>
#property (strong, nonatomic) UIScrollView *scrollView;
#property (strong, nonatomic) UIPageControl *pageControl;
#property (strong, nonatomic) UIButton *doneButton;
#property (strong, nonatomic) UIView *viewOne;
#property (strong, nonatomic) UIView *viewTwo;
#property (strong, nonatomic) UIView *viewThree;
#property (strong, nonatomic) UIView *viewFour;
#property (strong, nonatomic) UIView *viewFive;
#end
#implementation ABCIntroView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
[self addSubview:self.scrollView];
[self addSubview:self.pageControl];
[self.scrollView addSubview:self.viewOne];
[self.scrollView addSubview:self.viewTwo];
[self.scrollView addSubview:self.viewThree];
[self.scrollView addSubview:self.viewFour];
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
//Done Button
//[self addSubview:self.doneButton];
}
return self;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = CGRectGetWidth(self.bounds);
CGFloat pageFraction = self.scrollView.contentOffset.x / pageWidth;
self.pageControl.currentPage = roundf(pageFraction);
}
-(UIView *)viewOne {
if (!_viewOne) {
_viewOne = [[UIView alloc] initWithFrame:self.frame];
UIImageView *imageview;
imageview = [[UIImageView alloc] initWithFrame:self.frame];
imageview.image = [UIImage imageNamed:#"intro_slide_0"];
[_viewOne addSubview:imageview];
}
return _viewOne;
}
-(UIView *)viewTwo {
if (!_viewTwo) {
CGFloat originWidth = self.frame.size.width;
CGFloat originHeight = self.frame.size.height;
_viewTwo = [[UIView alloc] initWithFrame:CGRectMake(originWidth, 0, originWidth, originHeight)];
UIImageView *imageviewTwo;
imageviewTwo = [[UIImageView alloc] initWithFrame:self.frame];
imageviewTwo.image = [UIImage imageNamed:#"intro_slide_1"];
[_viewTwo addSubview:imageviewTwo];
}
return _viewTwo;
}
-(UIView *)viewThree{
if (!_viewThree) {
CGFloat originWidth = self.frame.size.width;
CGFloat originHeight = self.frame.size.height;
_viewThree = [[UIView alloc] initWithFrame:CGRectMake(originWidth*2, 0, originWidth, originHeight)];
UIImageView *imageviewThree;
imageviewThree = [[UIImageView alloc] initWithFrame:self.frame];
imageviewThree.image = [UIImage imageNamed:#"intro_slide_2"];
[_viewThree addSubview:imageviewThree];
}
return _viewThree;
}
-(UIView *)viewFour {
if (!_viewFour) {
CGFloat originWidth = self.frame.size.width;
CGFloat originHeight = self.frame.size.height;
_viewFour = [[UIView alloc] initWithFrame:CGRectMake(originWidth*3, 0, originWidth, originHeight)];
UIImageView *imageviewFour;
imageviewFour = [[UIImageView alloc] initWithFrame:self.frame];
imageviewFour.image = [UIImage imageNamed:#"intro_slide_3"];
[_viewFour addSubview:imageviewFour];
UIButton *btnDone = [[UIButton alloc] initWithFrame:CGRectMake(30, self.frame.size.height - 130, self.frame.size.width-60, 50)];
[btnDone setImage:[UIImage imageNamed:#"slide_3_button_unpressed"] forState:UIControlStateNormal];
[btnDone setImage:[UIImage imageNamed:#"slide_3_button_pressed"] forState:UIControlStateHighlighted];
[btnDone addTarget:self.delegate action:#selector(onDoneButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[_viewFour addSubview:btnDone];
}
return _viewFour;
}
-(UIView *)viewFive {
if (!_viewFive) {
}
return _viewFive;
}
-(UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
[_scrollView setDelegate:self];
[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(self.frame.size.width*numberofPage, self.scrollView.frame.size.height)];
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
return _scrollView;
}
-(UIPageControl *)pageControl {
if (!_pageControl) {
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height-20, self.frame.size.width, 10)];
//[_pageControl setCurrentPageIndicatorTintColor:UIColorFromRGBAlpha(252, 61, 136, 1)];
//[_pageControl setPageIndicatorTintColor:UIColorFromRGBAlpha(223, 227, 232, 1)];
[_pageControl setNumberOfPages:numberofPage];
}
return _pageControl;
}
-(UIButton *)doneButton {
if (!_doneButton) {
_doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.frame.size.height-60, self.frame.size.width, 100)];
[_doneButton setTintColor:[UIColor whiteColor]];
// [_doneButton.titleLabel setFont:FONT_Regular(17)];
[_doneButton setBackgroundColor:[UIColor colorWithRed:96/255.0 green:167/255.0 blue:23/255.0 alpha:1]];
[_doneButton addTarget:self.delegate action:#selector(onDoneButtonPressed) forControlEvents:UIControlEventTouchUpInside];
}
return _doneButton;
}
#end
Maybe what I am asking is not possible to be implemented.
Any clues and ideas are welcome
You can easily achieve your requirement by modifying ABCIntroView class. From ABCIntroView you are getting the callback as - (void)onDoneButtonPressed to IntroSlideViewController so there you are navigating to MainViewController.
So follow up these steps to achieve your requirement
Remove done button code from ABCIntroView.
Modify the below function.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat pageWidth = CGRectGetWidth(self.bounds);
CGFloat pageFraction = self.scrollView.contentOffset.x/pageWidth;
self.pageControl.currentPage = roundf(pageFraction);
if (self.delegate != nil && self.pageControl.currentPage == 4){
[self.delegate onDoneButtonPressed];
}}
Interface is define like this
#interface IGLDemoCustomView : UIView
#property (nonatomic, strong) UIImage *image;
#property (nonatomic, strong) NSString *title;
#end
While Implementation file look like this
#interface IGLDemoCustomView ()
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UILabel *titleLabel
#end
#implementation IGLDemoCustomView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
[self initView];
}
- (void)initView
{
self.layer.borderColor = [UIColor colorWithRed:0.18 green:0.59 blue:0.69 alpha:1.0].CGColor;
self.layer.borderWidth = 2.0;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
self.alpha = 0.8;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeCenter;
[self addSubview:imageView];
self.imageView = imageView;
UILabel *titleLabel;
self.titleLabel = [[UILabel alloc]init];
titleLabel.center = self.center; // set proper frame for label
[self addSubview:titleLabel];
}
- (void)setImage:(UIImage *)image
{
_image = image;
self.imageView.image = image;
}
- (void)setString:(NSString *)title
{
self.title=title;
self.titleLabel.text = title;
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
self.imageView.frame = self.bounds;
self.layer.cornerRadius = frame.size.height / 2.0;
}
#end
When i select image from the drop down menu it shows in the menu, but when i select any text from the drop down menu it doesnt show in the drop down list view.
Any clue will be highly appreciated.
Calling and setting string in the view
IGLDropDownItem *menuButton = strongSelf.dropDownMenu.menuButton;
IGLDemoCustomView *buttonView = (IGLDemoCustomView*)menuButton.customView;
buttonView.title = device.name;
You need to set a frame for the label
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
self.imageView.frame = self.bounds;
self.titleLabel.frame = self.bounds;
self.layer.cornerRadius = frame.size.height / 2.0;
}
If you are expecting the text to wrap, you'll need to set the lineBreakMode and set numberOfLines to 0
Like you are having UIImageView you need to have a UILabel inside your UIView too.
#interface IGLDemoCustomView ()
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UILabel *titleLabel;
#end
- (void)initView
{
self.layer.borderColor = [UIColor colorWithRed:0.18 green:0.59 blue:0.69 alpha:1.0].CGColor;
self.layer.borderWidth = 2.0;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
self.alpha = 0.8;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeCenter;
[self addSubview:imageView];
self.imageView = imageView;
self.titleLabel = [[UILabel alloc]init];
titleLabel.center = self.center; // set proper frame for label
[self addSubview:titleLabel]
}
And in setString
- (void)setString:(NSString *)title
{
self.title=title;
self.titleLabel.text = title;
}
I'm just beginner in Objective C.
I want to create multiple textfield with only bottom line border.
for this i crete a new class customTextField
look at my code;-
CustomTextField.m
- (id)init {
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor grayColor].CGColor;
border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, self.frame.size.height);
border.borderWidth = borderWidth;
[self.layer addSublayer:border];
self.layer.masksToBounds = YES;
return self;
}
in my loginViewController.h
#import <UIKit/UIKit.h>
#import "CustomTextField.h"
#interface LoginViewController : UIViewController
#property (strong, nonatomic) IBOutlet CustomTextField *txtFirstName;
#property (strong, nonatomic) IBOutlet CustomTextField *txtLastName;
#property (strong, nonatomic) IBOutlet CustomTextField *txtEmail;
#property (strong, nonatomic) IBOutlet CustomTextField *txtPassword;
#end
loginViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
_txtFirstName = [[CustomTextField alloc]init];
_txtLastName = [[CustomTextField alloc]init];
_txtEmail = [[CustomTextField alloc]init];
_txtPassword = [[CustomTextField alloc]init];
}
I don't know where I do mistakes. this code is doing nothing. Can any one tell me where I do wrong ?
any help or any suggestion.
Try with this:
self.layer.masksToBounds = YES;
self.layer.borderColor = [UIColor grayColor].CGColor;
self.layer.borderWidth = borderWidth;
But you're using IBOulet, which means your objects are initialised from the storyboard, so then you should move your code to awakeFromNib
- (void)awakeFromNib
{
self.layer.masksToBounds = YES;
self.layer.borderColor = [UIColor grayColor].CGColor;
self.layer.borderWidth = borderWidth;
}
Add a private border layer property in your .m file
#property (strong, nonatomic) IBOutlet CALayer *borderLayer;
Override layoutSubviews to set up the border.
- (void)layoutSubViews {
[super layoutSubViews];
if (self.borderLayer) {
self.borderLayer = [CALayer layer];
[self.layer addSublayer:border];
}
CGFloat borderWidth = 5.0f;
self.borderLayer.backgroundColor = color.CGColor;
self.borderLayer.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, borderWidth);
}
Update:
Don't initialize the CustomTextField IBOutlets in viewDidLoad. Simply set their class to CustomTextField in the storyboard!
you are using subclass with storyboard your code will fine if you create all button programatically but if you want that your code will work fine with story board and programatically then you have to use this code in your CustomTextField.m
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
[self initialize];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
// Initialization code
}
return self;
}
- (void) awakeFromNib
{
[super awakeFromNib];
}
- (void) layoutSubviews
{
for (UIView *view in self.subviews) {
[view removeFromSuperview];
}
[self initialize];
}
- (instancetype)init
{
self = [super init];
if (self) {
self = [self initWithFrame:self.frame];
}
return self;
}
-(void)initialize
{
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor grayColor].CGColor;
border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, self.frame.size.height);
border.borderWidth = borderWidth;
[self.layer addSublayer:border];
self.layer.masksToBounds = YES;
}
I have a view controller (vc) with a UIPickerView on it which needs to slide up from the bottom half of the screen.
Using:
ViewControlelr *vc = [self.storyboard instantiateViewControllerWithIdentifier: #"VC"];
[self presentViewController:vc animated:YES completion:nil]
makes the page display in fullscreeen
Ive read that putting UIPickerViews in activity sheets is not a good idea anymore, any suggestions?
Ive read that putting UIPickerViews in activity sheets is not a good
idea anymore, any suggestions?
In all honesty, it was never a good idea but it became kind of an underground standard. If you are displaying it after the user sets the text field as the firstResponder, you can set the UIPickerView as the UITextField's inputView. If you want a tool bar on top of it, create one and set it as the UITextField's inputAccessoryView.
If this is not the case, I would suggest creating a UIView container to hold the UIPickerView and set its y to the height of the screen. When you want to show it, use UIView animations to change the y to the height of screen minus the height of the container.
Alternatively you could just do the same thing without a UIView container but you may lose some control over how you want to present the UIPickerView since its size is fixed.
EDIT 1:
I created a container that holds a UIPickerView though it seems to only be presented correctly presentable in portrait mode. Feel free to expand upon it to make it orientation friendly.
//
// AbstractPickerPresenter.h
// ShowDatePickerView
//
// Created by Christopher John Morris on 10/10/13.
// Copyright (c) 2013 Christopher John Morris. All rights reserved.
//
#import <UIKit/UIKit.h>
#protocol PickerPresenterDelegate;
#protocol PickerPresenterButtonDelegate;
#interface PickerPresenter : UIView {
UIView *pickerContainer;
id<PickerPresenterButtonDelegate>buttonDelegate;
}
/*
* Helper constant used for measuring frames
*/
#property (nonatomic, readonly) CGFloat pickerHeight;
/*
* Helper constant used for measuring frames
*/
#property (nonatomic, readonly) CGFloat navBarHeight;
/*
* Automatically hides view when Cancel is clicked.
* Default is YES
*/
#property (nonatomic) BOOL hideOnDoneClicked;
/*
* Automatically hides view when Done is clicked.
* Default is YES
*/
#property (nonatomic) BOOL hideOnCancelClicked;
/*
* Raises the view into visible position
*/
- (void) show;
/*
* Removes the view out of visible position
*/
- (void) hide;
/*
* Sets titles for buttons. Size of buttons adujst based on textsize
*/
- (void)setCancelButtonTitle:(NSString *)title;
- (void)setDoneButtonTitle:(NSString *)title;
#end
/*
* Delegate that is used strictly by subclasses to react
* to button presses.
*/
#protocol PickerPresenterButtonDelegate
#required
- (void) didCancel;
- (void) didSave;
#end
...
//
// AbstractPickerPresenter.m
// ShowDatePickerView
//
// Created by Christopher John Morris on 10/10/13.
// Copyright (c) 2013 Christopher John Morris. All rights reserved.
//
#import "PickerPresenter.h"
#interface PickerPresenter()
/*
* Tint color of buttons
*/
#property (nonatomic, strong) UIColor *tintColor;
/*
* Cancel button
*/
#property (nonatomic, strong) UIButton *cancelButton;
/*
* Done button
*/
#property (nonatomic, strong) UIButton *doneButton;
/*
* Black facade that appears behind the picker container
* and is the size of the current UIWindow
*/
#property (nonatomic, strong) UIView *facadeView;
/*
* Helper constant used to make animations consistent
*/
#property (nonatomic, readonly) NSTimeInterval animationInterval;
#end
#implementation PickerPresenter
- (id)init {
self = [super init];
if (self) {
[self initializeView];
[self initializeViewContainer];
[self initializeCancelButton];
[self initializeDoneButton];
}
return self;
}
- (void) initializeView {
[self setHidden:YES];
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
self.frame = CGRectMake(0, height, [[UIScreen mainScreen] bounds].size.width, height);
self.backgroundColor = [UIColor clearColor];
self.tintColor = [[[[UIApplication sharedApplication] delegate] window] tintColor];
self.facadeView = [[UIView alloc] initWithFrame:self.frame];
self.facadeView.backgroundColor = [UIColor blackColor];
self.facadeView.alpha = 0.4;
[self addSubview:self.facadeView];
self.hideOnDoneClicked = YES;
self.hideOnCancelClicked = YES;
}
- (void) initializeViewContainer {
CGFloat containerHeight = (self.pickerHeight + self.navBarHeight);
pickerContainer = [[UIView alloc] initWithFrame:CGRectMake(0,
self.frame.size.height + containerHeight,
self.frame.size.width,
containerHeight)];
pickerContainer.backgroundColor = [UIColor whiteColor];
[self addSubview:pickerContainer];
}
- (void) initializeCancelButton {
self.cancelButton = [[UIButton alloc] init];
[self.cancelButton setTitleColor:self.tintColor forState:UIControlStateNormal];
[self.cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
self.cancelButton.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:20];
CGSize size = [self.cancelButton.titleLabel.text sizeWithFont:self.cancelButton.titleLabel.font];
self.cancelButton.frame = CGRectMake(10, 0, size.width, self.navBarHeight);
[self.cancelButton addTarget:self action:#selector(cancel) forControlEvents:UIControlEventTouchUpInside];
[pickerContainer addSubview:self.cancelButton];
}
- (void) cancel {
[buttonDelegate didCancel];
}
- (void) done {
[buttonDelegate didSave];
}
- (void) initializeDoneButton {
self.doneButton = [[UIButton alloc] init];
[self.doneButton setTitleColor:self.tintColor forState:UIControlStateNormal];
[self.doneButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.doneButton setTitle:#"Done" forState:UIControlStateNormal];
self.doneButton.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:20];
[self.doneButton addTarget:self action:#selector(done) forControlEvents:UIControlEventTouchUpInside];
CGSize size = [self.doneButton.titleLabel.text sizeWithFont:self.doneButton.titleLabel.font];
self.doneButton.frame = CGRectMake(self.frame.size.width - (size.width + 10), 0, size.width, self.navBarHeight);
[pickerContainer addSubview:self.doneButton];
}
- (void)setCancelButtonTitle:(NSString *)title {
[self.cancelButton setTitle:title forState:UIControlStateNormal];
CGSize size = [self.cancelButton.titleLabel.text sizeWithFont:self.cancelButton.titleLabel.font];
self.cancelButton.frame = CGRectMake(10,
0,
size.width,
self.cancelButton.frame.size.height);
}
- (void)setDoneButtonTitle:(NSString *)title {
[self.doneButton setTitle:title forState:UIControlStateNormal];
CGSize size = [self.doneButton.titleLabel.text sizeWithFont:self.doneButton.titleLabel.font];
self.doneButton.frame = CGRectMake(self.frame.size.width - (size.width + 10),
self.doneButton.frame.origin.y,
size.width,
self.doneButton.frame.size.height);
}
- (void) show {
self.alpha = 0.0;
self.frame = CGRectMake(self.frame.origin.x,
self.frame.origin.y - self.frame.size.height,
self.frame.size.width,
self.frame.size.height);
self.facadeView.frame = CGRectMake(self.frame.origin.x,
self.frame.origin.y,
self.frame.size.width,
self.frame.size.height);
[[[UIApplication sharedApplication] keyWindow] addSubview:self];
[UIView animateWithDuration:self.animationInterval animations:^{
[self setHidden:NO];
self.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:self.animationInterval animations:^{
pickerContainer.frame = CGRectMake(pickerContainer.frame.origin.x,
self.frame.size.height - pickerContainer.frame.size.height,
pickerContainer.frame.size.width,
pickerContainer.frame.size.height);
} completion:nil];
}];
}
- (void) hide {
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
[UIView animateWithDuration:self.animationInterval animations:^{
pickerContainer.frame = CGRectMake(0,
self.frame.size.height + pickerContainer.frame.size.height,
self.frame.size.width,
pickerContainer.frame.size.height);
} completion:^(BOOL finished) {
[UIView animateWithDuration:self.animationInterval animations:^{
self.alpha = 0.0;
} completion:^(BOOL finished) {
[self setHidden:YES];
[self removeFromSuperview];
self.frame = CGRectMake(self.frame.origin.x,
height,
self.frame.size.width,
self.frame.size.height);
self.facadeView.frame = CGRectMake(self.frame.origin.x,
self.frame.origin.y,
self.frame.size.width,
self.frame.size.height);
}];
}];
}
- (CGFloat)pickerHeight {
CGFloat const kPickerHeight = 216;
return kPickerHeight;
}
-(CGFloat)navBarHeight {
CGFloat const kNavBarHeight = 44;
return kNavBarHeight;
}
-(NSTimeInterval)animationInterval {
NSTimeInterval const kAnimationInterval = 0.2;
return kAnimationInterval;
}
#end
...
//
// DataPickerPresenter.h
// ShowDatePickerView
//
// Created by Christopher John Morris on 10/11/13.
// Copyright (c) 2013 Christopher John Morris. All rights reserved.
//
#import "PickerPresenter.h"
#protocol DataPickerPresenterDelegate;
#interface UIPickerViewPresenter : PickerPresenter
/*
* Singleton instance of class
*/
+ (id) defaultPresenter;
/*
* Calls back to register button clicks
*/
#property (nonatomic, strong) id<DataPickerPresenterDelegate>delegate;
- (void) setDataArrayWithArray:(NSArray *)array;
#end
/*
* Delegate that is used to send messages from subclasses
* to classes using the subclass.
*/
#protocol DataPickerPresenterDelegate
#required
- (void) dataPickerPresenerClickedCancel:(UIPickerViewPresenter *)picker;
- (void) dataPickerPresenerClickedDone:(UIPickerViewPresenter *)picker withString:(NSString *)string index:(NSInteger)index;
#end
...
//
// DataPickerPresenter.m
// ShowDatePickerView
//
// Created by Christopher John Morris on 10/11/13.
// Copyright (c) 2013 Christopher John Morris. All rights reserved.
//
#import "UIPickerViewPresenter.h"
#interface UIPickerViewPresenter() <PickerPresenterButtonDelegate, UIPickerViewDelegate, UIPickerViewDataSource>
#property (nonatomic, strong) UIPickerView *picker;
#property (nonatomic, strong) NSArray *pickerData;
#end
#implementation UIPickerViewPresenter
+(id)defaultPresenter {
static UIPickerViewPresenter *defaultPresenter = nil;
if (!defaultPresenter) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
defaultPresenter = [[super allocWithZone:NULL] init];
});
}
return defaultPresenter;
}
+ (id)allocWithZone:(NSZone *)zone {
return [self defaultPresenter];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)init {
self = [super init];
if (self) {
[self initializePicker];
buttonDelegate = self;
}
return self;
}
- (void) initializePicker {
self.picker = [[UIPickerView alloc] init];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect frame = self.picker.frame;
frame.origin.y = frame.origin.y + self.navBarHeight;
frame.size.width = screenRect.size.width;
self.picker.frame = frame;
self.picker.delegate = self;
self.picker.dataSource = self;
[pickerContainer addSubview:self.picker];
}
-(void)didCancel {
if (self.hideOnCancelClicked) {
[self hide];
}
[self.delegate dataPickerPresenerClickedCancel:self];
}
-(void)didSave {
if (self.hideOnDoneClicked) {
[self hide];
}
NSInteger selectedRow = [self.picker selectedRowInComponent:0];
NSString *selection = [self.pickerData objectAtIndex:selectedRow];
[self.delegate dataPickerPresenerClickedDone:self withString:selection index:selectedRow];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.pickerData.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [self.pickerData objectAtIndex:row];
}
- (void)setDataArrayWithArray:(NSArray *)array {
self.pickerData = array;
[self.picker reloadAllComponents];
}
#end
To initialize it, write:
UIPickerViewPresenter *presenter = [UIPickerViewPresenter defaultPresenter];
[presenter setDataArrayWithArray:self.dataArray];
where 'self.dataArray' is the data you would like to use to populate the UIPickerView. To display it, call:
[presenter show];
When you have an UITableView perhaps you can use this: https://github.com/aberger/ABMExpandingTableViewCells
I'm trying to create a text field with a persistent "placeholder" of sorts by adding a UILabel as a subview of the text field. However, my label keeps being truncated, and I can't figure out why. Any advice is much appreciated!
Here's what I'm currently getting (it's supposed to say "first name":
Here's the code I have:
#import "LabeledTextField.h"
#interface PlaceholderLabel : UILabel
#end
#implementation PlaceholderLabel
-(void)drawTextInRect:(CGRect)rect
{
UIEdgeInsets insets = {0, 10, 0, 0};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
#end
#interface LabeledTextField ()
#property (nonatomic) PlaceholderLabel *placeholderLabel;
#end
#implementation LabeledTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
_placeholderLabel = [[PlaceholderLabel alloc] initWithFrame:CGRectMake(10, [self frame].size.height/2, 0, 0)];
[_placeholderLabel setBackgroundColor:UIColorFromRGB(0xdedede)];
[_placeholderLabel setTextColor:UIColorFromRGB(0x787878)];
[_placeholderLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:kTextFieldPlaceholderFontSize]];
[_placeholderLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[_placeholderLabel setNumberOfLines:1];
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
[self setLeftView:_placeholderLabel];
}
-(void)drawPlaceholderInRect:(CGRect)rect
{
return;
}
-(void)setPlaceholder:(NSString *)placeholder
{
[_placeholderLabel setText:placeholder];
[_placeholderLabel sizeToFit];
}
-(CGRect)leftViewRectForBounds:(CGRect)bounds
{
CGRect frame = [_placeholderLabel frame];
frame.origin.y = (self.frame.size.height - frame.size.height)/2;
frame.size = [[_placeholderLabel text] sizeWithFont:[UIFont fontWithName:#"HelveticaNeue" size:kTextFieldPlaceholderFontSize]];
return frame;
}
#end