UITextField can't edit,focus - ios

//
// SignStepRemindView.m
// LTPMS
//
// Created by longdw on 16/7/14.
// Copyright © 2016年 longdw. All rights reserved.
//
#define kPadding 10
#import "SignStepRemindView.h"
#import "SignStepRemindCell.h"
#import "UITableViewCell+AutoHeightForCell.h"
#import "NSString+Frame.h"
#interface SignStepRemindView() <UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
#property(nonatomic, strong) UIWindow *window;
#property(nonatomic, strong) UIView *backIv;
#property(nonatomic, weak) UIView *contentView;
#property(nonatomic, weak) UITableView *tableView;
#property(nonatomic, weak) UIScrollView *scrollView;
#property(nonatomic, weak) UIView *remindContentView;
#property(nonatomic, weak) UIButton *leftBtn;
#property(nonatomic, weak) UIButton *rightBtn;
#property(nonatomic, weak) UILabel *titleLabel;
#property(nonatomic, assign) NSInteger step;
#property(nonatomic, weak) UITextField *textField;
#end
#implementation SignStepRemindView
- (instancetype)init
{
if (self = [super init]) {
self.backgroundColor = kGlobalBgColor;
[self addViews];
}
return self;
}
- (void)addViews
{
self.layer.cornerRadius = 4;
self.layer.masksToBounds = YES;
UILabel *titleLabel = [[UILabel alloc] init];
self.titleLabel = titleLabel;
titleLabel.text = #"select step";
titleLabel.textColor = kNavColor;
titleLabel.font = [UIFont systemFontOfSize:16];
[self addSubview:titleLabel];
WEAKSELF
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.equalTo(weakSelf).offset(kPadding);
make.height.mas_equalTo(30);
}];
UIView *bottomView = [[UIView alloc] init];
[self addSubview:bottomView];
[bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(weakSelf).offset(-kPadding * 0.5);
make.left.equalTo(weakSelf);
make.right.equalTo(weakSelf).offset(-kPadding);
make.height.mas_equalTo(40);
}];
UIButton *rightBtn = [[UIButton alloc] init];
self.rightBtn = rightBtn;
rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
[rightBtn setTitleColor:kNavColor forState:UIControlStateNormal];
[rightBtn setTitle:#"next" forState:UIControlStateNormal];
[rightBtn addTarget:self action:#selector(next) forControlEvents:UIControlEventTouchUpInside];
[bottomView addSubview:rightBtn];
[rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(bottomView);
make.height.equalTo(bottomView);
make.centerY.equalTo(bottomView.mas_centerY);
make.width.mas_equalTo(60);
}];
UIButton *leftBtn = [[UIButton alloc] init];
self.leftBtn = leftBtn;
leftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
[leftBtn setTitleColor:kNavColor forState:UIControlStateNormal];
[leftBtn setTitle:#"cancel" forState:UIControlStateNormal];
[leftBtn addTarget:self action:#selector(cancel) forControlEvents:UIControlEventTouchUpInside];
[bottomView addSubview:leftBtn];
[leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(rightBtn.mas_left).offset(-kPadding);
make.height.equalTo(bottomView);
make.centerY.equalTo(bottomView.mas_centerY);
make.width.mas_equalTo(60);
}];
UIScrollView *scrollView = [[UIScrollView alloc] init];
self.scrollView = scrollView;
scrollView.delegate = self;
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
[self addSubview:scrollView];
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(titleLabel.mas_bottom).offset(kPadding);
make.bottom.equalTo(bottomView.mas_top);
make.left.equalTo(weakSelf).offset(kPadding);
make.right.equalTo(weakSelf).offset(-kPadding);
}];
UIView *contentView = [[UIView alloc] init];
self.contentView = contentView;
[self.scrollView addSubview:contentView];
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(scrollView);
make.height.equalTo(scrollView);
}];
//first
UITableView *tableView = [[UITableView alloc] init];
tableView.backgroundColor = [UIColor redColor];
self.tableView = tableView;
// tableView.delegate = self;
// tableView.dataSource = self;
UIView *view = [UIView new];
view.backgroundColor = [UIColor clearColor];
[tableView setTableFooterView:view];
[contentView addSubview:tableView];
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.equalTo(contentView);
make.width.equalTo(weakSelf.scrollView);
}];
//second
UIScrollView *remindScrollView = [[UIScrollView alloc] init];
[contentView addSubview:remindScrollView];
[remindScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(tableView.mas_right);
make.top.bottom.equalTo(contentView);
make.width.equalTo(weakSelf.scrollView);
}];
UIView *remindContentView = [[UIView alloc] init];
self.remindContentView = remindContentView;
[remindScrollView addSubview:remindContentView];
[remindContentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(remindScrollView);
make.width.equalTo(remindScrollView);
}];
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(remindScrollView.mas_right);
}];
[self addRemindView];
}
- (void)addRemindView
{
UIView *editView = [[UIView alloc] init];
editView.userInteractionEnabled = YES;
[self.remindContentView addSubview:editView];
WEAKSELF
[editView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(weakSelf.remindContentView).offset(kPadding);
make.right.equalTo(weakSelf.remindContentView).offset(-kPadding);
make.height.mas_equalTo(40);
}];
NSString *title = #"content:";
UIFont *font = [UIFont systemFontOfSize:15];
CGFloat width = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:#{ NSFontAttributeName : font }].width;
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = font;
titleLabel.text = title;
[editView addSubview:titleLabel];
UITextField *textField = [[UITextField alloc] init];
self.textField = textField;
[self.textField setEnabled:YES];
// [textField becomeFirstResponder];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.backgroundColor = [UIColor whiteColor];
[editView addSubview:textField];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(editView);
make.centerY.equalTo(editView.mas_centerY);
make.width.mas_equalTo(width);
}];
[textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(titleLabel.mas_right);
make.top.bottom.equalTo(editView);
make.centerY.equalTo(editView.mas_centerY);
make.right.equalTo(editView);
}];
}
#pragma mark - UITableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (SignStepRemindCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
SignStepRemindCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[SignStepRemindCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [UITableViewCell cellHeightForIndexPath:indexPath tableView:tableView] + 10;
}
- (void)show:(UIView *)view
{
// UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIWindow *window = view.window;
// self.window = window;
// window.windowLevel = UIWindowLevelNormal;
// window.backgroundColor = [UIColor clearColor];
// window.alpha = 1;
// window.hidden = NO;
[window makeKeyAndVisible];
NSLog(#"key1-->%#, key2-->%#", [UIApplication sharedApplication].keyWindow, window);
UIView *backIv = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.backIv = backIv;
backIv.backgroundColor = [UIColor blackColor];
backIv.alpha = 0.7;
[window addSubview:backIv];
[window addSubview:self];
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height * 0.7;
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(30);
make.right.mas_equalTo(-30);
make.height.mas_equalTo(screenHeight);
make.center.equalTo(window);
}];
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
- (void)dismiss
{
[self.backIv removeFromSuperview];
[self removeFromSuperview];
}
#pragma mark - 滚动代理
#pragma mark scrollview减速完毕就会调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//获取当前页码
int pageNo = scrollView.contentOffset.x / scrollView.frame.size.width;
if (pageNo == 0) {
[self.leftBtn setTitle:#"cancel" forState:UIControlStateNormal];
[self.rightBtn setTitle:#"next" forState:UIControlStateNormal];
self.titleLabel.text = #"select step";
self.step = 0;
} else {
[self.leftBtn setTitle:#"pre" forState:UIControlStateNormal];
[self.rightBtn setTitle:#"finish" forState:UIControlStateNormal];
self.titleLabel.text = #"select person";
self.step = 1;
}
}
- (void)cancel
{
if (self.step == 0) {//cancel
[self dismiss];
} else {//pre
self.leftBtn.enabled = NO;
self.rightBtn.enabled = NO;
CGPoint offset = self.scrollView.contentOffset;
offset.x -= self.scrollView.frame.size.width;
[UIView animateWithDuration:0.25 animations:^{
self.scrollView.contentOffset = offset;
} completion:^(BOOL finished) {
[self.leftBtn setTitle:#"cancel" forState:UIControlStateNormal];
[self.rightBtn setTitle:#"next" forState:UIControlStateNormal];
self.titleLabel.text = #"select step";
self.step = 0;
self.leftBtn.enabled = YES;
self.rightBtn.enabled = YES;
}];
}
}
- (void)next
{
if (self.step == 0) {//next
self.leftBtn.enabled = NO;
self.rightBtn.enabled = NO;
CGPoint offset = self.scrollView.contentOffset;
offset.x += self.scrollView.frame.size.width;
[UIView animateWithDuration:0.25 animations:^{
self.scrollView.contentOffset = offset;
} completion:^(BOOL finished) {
[self.leftBtn setTitle:#"pre" forState:UIControlStateNormal];
[self.rightBtn setTitle:#"finish" forState:UIControlStateNormal];
self.titleLabel.text = #"select person";
self.step = 1;
self.leftBtn.enabled = YES;
self.rightBtn.enabled = YES;
}];
} else {//finish
}
}
- (void)dealloc
{
// NSArray *subViews = [self.window subviews];
// for (id obj in subViews) {
// [obj removeFromSuperview];
// }
// [self.window resignKeyWindow];
// [self.window removeFromSuperview];
// self.window = nil;
}
#end
this is custom cell
#define kPadding 10
#import "SignStepRemindCell.h"
#interface SignStepRemindCell()
#property(nonatomic, strong) UIImage *unSelectedImage;
#property(nonatomic, strong) UIImage *selectedImage;
#end
#implementation SignStepRemindCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.unSelectedImage = [UIImage imageNamed:#"contact_icon_checkbox"];
self.selectedImage = [UIImage imageNamed:#"contact_icon_checkbox_selected_all"];
[self addViews];
}
return self;
}
- (void)addViews
{
UIImageView *checkImageView = [[UIImageView alloc] initWithImage:self.selectedImage];
[self.contentView addSubview:checkImageView];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [UIFont systemFontOfSize:15];
[self.contentView addSubview:titleLabel];
UILabel *subTitleLabel = [[UILabel alloc] init];
subTitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
subTitleLabel.numberOfLines = 0;
subTitleLabel.textColor = [UIColor blackColor];
subTitleLabel.font = [UIFont systemFontOfSize:15];
[self.contentView addSubview:subTitleLabel];
WEAKSELF
[checkImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.contentView).offset(kPadding);
make.centerY.equalTo(weakSelf.contentView.mas_centerY);
make.size.mas_equalTo(weakSelf.selectedImage.size);
}];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.contentView).offset(kPadding);
make.left.equalTo(checkImageView.mas_right).offset(kPadding);
make.right.equalTo(weakSelf.contentView).offset(-kPadding);
make.height.mas_greaterThanOrEqualTo(0);
}];
[subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(titleLabel.mas_bottom).offset(5);
make.left.equalTo(titleLabel.mas_left);
make.right.equalTo(weakSelf.contentView).offset(-kPadding);
make.height.mas_greaterThanOrEqualTo(0);
}];
titleLabel.text = #"num:2";
NSString *text = #"<strong>test:</strong>test1, test2, test3, test4";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[text dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
subTitleLabel.attributedText = attrStr;
}
#end
I use this custom view in vc
SignStepRemindView *stepRemindView = [[SignStepRemindView alloc] init];
[stepRemindView show:self.view];
My question is the UITextField can't edit and touch,the keyboard has never been shown,but when I use [textField becomeFirstResponder],I can input,the keyboard show,but the cursor does not show.
Thank you very much.

I solved it.The reason is that constraints are not complete.
Add the following code at the end of the method addRemindView
[self.remindContentView mas_makeConstraints:^(MASConstraintMaker *make){
make.bottom.equalTo(editView.mas_bottom).offset(20);
}];

Related

dynamic cell height for custom uitableviewcell not working

I created a custom tableview cell and the code is given below. I am trying to add a new UIView named bottomTextContainer to the contentview of the cell.The bottomTextContainer holds the labels of published date and source of the article. my problem is bottomTextContainer view is not showing inside the cell it is overlapping with the next cell.
ArticleCell.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
#interface ArticleCell : UITableViewCell
#pragma mark- Properties
#property (strong, nonatomic) UILabel *source;
#property (strong, nonatomic) UILabel *publishedDate;
#pragma mark- Methods
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier;
#end
NS_ASSUME_NONNULL_END
ArticleCell.m
#import "ArticleCell.h"
#interface ArticleCell ()
#pragma mark- Private Properties
#property (strong, nonatomic) UIView *bottomTextContainer;
#end
#implementation ArticleCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self createSubViews];
}
return self;
}
- (void)createSubViews{
self.textLabel.numberOfLines = 0;
self.textLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:20];
self.textLabel.textColor = [UIColor blackColor];
self.textLabel.textAlignment = NSTextAlignmentLeft;
self.detailTextLabel.numberOfLines = 0;
self.detailTextLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:16];
self.detailTextLabel.textColor = [UIColor blackColor];
self.detailTextLabel.textAlignment = NSTextAlignmentLeft;
self.bottomTextContainer = [[UIView alloc] init];
[self.contentView addSubview:self.bottomTextContainer];
self.bottomTextContainer.translatesAutoresizingMaskIntoConstraints = false;
self.source = [[UILabel alloc] init];
[self.bottomTextContainer addSubview:self.source];
self.source.textColor = [UIColor blackColor];
self.source.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12];
self.source.textAlignment = NSTextAlignmentLeft;
self.source.translatesAutoresizingMaskIntoConstraints = false;
self.publishedDate = [[UILabel alloc] init];
[self.bottomTextContainer addSubview:self.publishedDate];
self.publishedDate.textColor = [UIColor blackColor];
self.publishedDate.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12];
self.publishedDate.textAlignment = NSTextAlignmentRight;
self.publishedDate.translatesAutoresizingMaskIntoConstraints = false;
}
- (void)layoutSubviews{
[super layoutSubviews];
UILayoutGuide *contentViewLayout = self.contentView.layoutMarginsGuide;
[self.bottomTextContainer.topAnchor constraintEqualToAnchor:self.detailTextLabel.bottomAnchor].active = YES;
[self.bottomTextContainer.leadingAnchor constraintEqualToAnchor:contentViewLayout.leadingAnchor].active = YES;
[self.bottomTextContainer.trailingAnchor constraintEqualToAnchor:contentViewLayout.trailingAnchor].active = YES;
[self.source.topAnchor constraintEqualToAnchor:self.bottomTextContainer.topAnchor constant:10].active = YES;
[self.source.bottomAnchor constraintEqualToAnchor:self.bottomTextContainer.bottomAnchor constant:10].active = YES;
[self.source.leadingAnchor constraintEqualToAnchor:self.bottomTextContainer.leadingAnchor].active = YES;
[self.source.widthAnchor constraintEqualToAnchor:self.bottomTextContainer.widthAnchor multiplier:0.5].active = YES;
[self.publishedDate.topAnchor constraintEqualToAnchor:self.bottomTextContainer.topAnchor constant:10].active = YES;
[self.publishedDate.bottomAnchor constraintEqualToAnchor:self.bottomTextContainer.bottomAnchor constant:10].active = YES;
[self.publishedDate.trailingAnchor constraintEqualToAnchor:self.bottomTextContainer.trailingAnchor].active = YES;
[self.publishedDate.leadingAnchor constraintEqualToAnchor:self.source.trailingAnchor].active = YES;
}
#end
This is the result I am getting. Show
Try this:
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 44;

Add customview in navigation bar like whatsapp

I want to create custom navigation bar like WhatsApp uses to display call indicator in the application as given below.
I have successfully added view like above but it's not responsive because I am not able to detect touch on status bar. I can touch only part below "Touch to return to call".
Code is as given below.
#property (nonatomic) UIView *navigationBarTopView;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (_navigationBarTopView == nil) {
_navigationBarTopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, window.frame.size.width, 60.0)];
[_navigationBarTopView setBackgroundColor:[UIColor colorWithRed:76.0/255.0 green:217.0/255.0 blue:100.0/255.0 alpha:1]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, _navigationBarTopView.frame.size.height - 15, window.frame.size.width, 10)];
[label setText:#"Touch to return to call"];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
[label setTextAlignment:NSTextAlignmentCenter];
[_navigationBarTopView addSubview:label];
//The setup code (in viewDidLoad in your view controller)
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[_navigationBarTopView addGestureRecognizer:singleFingerTap];
UITapGestureRecognizer *singleFingerTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[label addGestureRecognizer:singleFingerTap1];
}
[window addSubview:_navigationBarTopView];
I have also tried to add touch on status bar as give below but it doesn't work.
How do I detect touches on UIStatusBar/iPhone
Also navigation bar is not coming downside. I have tried to set keyWindow frame. But that is also not working.
UIStatusBar has higher priority than your application's window, so you won't get any touch event through status bar.
To get touch event through status bar you need a window with higher window level than UIStatusBar.
Try below code:
#import "AppDelegate.h"
#interface AppDelegate ()
#property (nonatomic) UIWindow *buttonWindow;
#property (nonatomic) UIWindow *textWindow;
#property (nonatomic) CGFloat callViewHeight;
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if (#available(iOS 11.0, *)) {
if (([[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0)) {
self.callViewHeight = 55.0f;
} else {
self.callViewHeight = 35.0f;
}
} else {
self.callViewHeight = 35.0f;
}
return YES;
}
-(void)showVideoCallButton{
self.textWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
self.textWindow.windowLevel = self.window.windowLevel + 1;
self.textWindow.hidden = FALSE;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
view.backgroundColor = [UIColor colorWithRed:76.0/255.0 green:217.0/255.0 blue:100.0/255.0 alpha:1];
view.clipsToBounds = TRUE;
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
lblName.textAlignment = NSTextAlignmentCenter;
[lblName setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
lblName.text = #"";
UILabel *lblTouch = [[UILabel alloc] initWithFrame:CGRectMake(0, self.callViewHeight - 20.0f, [UIScreen mainScreen].bounds.size.width, 20.0f)];
lblTouch.textAlignment = NSTextAlignmentCenter;
[lblTouch setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
lblTouch.text = #"Touch to return to call";
lblTouch.textColor = UIColor.whiteColor;
[view addSubview:lblTouch];
[view addSubview:lblName];
[self.textWindow addSubview:view];
self.buttonWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
self.buttonWindow.windowLevel = UIWindowLevelStatusBar + 1;
self.buttonWindow.hidden = FALSE;
UIButton *button = [[UIButton alloc] initWithFrame:lblName.bounds];
[button setTitle:#"" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside];
[self.buttonWindow addSubview:button];
self.textWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
self.buttonWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
[UIView animateWithDuration:0.25
animations:^{
self.textWindow.transform = CGAffineTransformIdentity;
self.buttonWindow.transform = CGAffineTransformIdentity;
if (#available(iOS 11.0, *)) {
if (([[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0)) {
self.window.frame = CGRectMake(0, self.callViewHeight - 40.0f, self.window.bounds.size.width, self.window.bounds.size.height - self.callViewHeight + 40.0f);
} else {
self.window.frame = CGRectMake(0, self.callViewHeight - 20.0f, self.window.bounds.size.width, self.window.bounds.size.height - self.callViewHeight + 20.0f);
}
} else {
self.window.frame = CGRectMake(0, self.callViewHeight - 20.0f, self.window.bounds.size.width, self.window.bounds.size.height - self.callViewHeight + 20.0f);
}
}];
}
-(void)hideVideoCallButton{
[UIView animateWithDuration:0.25
animations:^{
self.textWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
self.buttonWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
self.window.frame = [UIScreen mainScreen].bounds;
} completion:^(BOOL finished) {
dispatch_async(dispatch_get_main_queue(), ^{
self.buttonWindow.hidden = TRUE;
self.buttonWindow = nil;
self.textWindow.hidden = TRUE;
self.textWindow = nil;
});
}];
}
-(void)buttonTouched{
NSLog(#"Button Touched");
}
#end
I tried in singleViewcontroller and UITabbar Controller also, the sample project I attached here, for status bar Tap action I followed Flar answer
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
[self createDummyView];
});
}
-(void)createDummyView{
if (_navigationBarTopView == nil) {
float statusBarHeight = [self statusBarHeight];
CGFloat width = [UIScreen mainScreen].bounds.size.width;
_navigationBarTopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, self.navigationController.navigationBar.frame.size.height + statusBarHeight)];
[_navigationBarTopView setBackgroundColor:[UIColor colorWithRed:76.0/255.0 green:217.0/255.0 blue:100.0/255.0 alpha:1]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, _navigationBarTopView.frame.size.height - 15,width, 10)];
[label setText:#"Touch to return to call"];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setUserInteractionEnabled:YES];
[_navigationBarTopView addSubview:label];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[_navigationBarTopView addGestureRecognizer:singleFingerTap];
[self.navigationController.view addSubview:_navigationBarTopView];
}
-(float) statusBarHeight
{
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
return MIN(statusBarSize.width, statusBarSize.height);
}
-(void)handleSingleTap:(UITapGestureRecognizer*)recognizer{
NSLog(#"recognizer == %#", recognizer.view.tag);
}

Switching views in UISegmentedControl in UIView

I am stuck up with a problem to switch views for UISegmentedControl. I have imported a framework of HMSegmentedControl to use multiple views (7 views). I want to switch views when tapping on New Matches it should display matches view, and when tapping on the Daily Recommendations, it should display matches view and so on. Below is my code. I have tried a lot in switching view by segmentedControlChangedValue, but it doesn't work for me. Anything to do or am I doing wrong?
#interface MatchesViewController ()
#property (nonatomic, strong) UIScrollView *scrollView;
#property (strong, nonatomic) IBOutlet UIScrollView *scrollVw;
#property (strong, nonatomic) IBOutlet UIView *matchesView;
#property (strong, nonatomic) IBOutlet UIView *dailyRecommendationsView;
#property (strong, nonatomic) IBOutlet UIView *preferedMatchesView;
#property (strong, nonatomic) IBOutlet UIView *broaderMatchesView;
#property (strong, nonatomic) IBOutlet UIView *twowayMatcheVsiew;
#property (strong, nonatomic) IBOutlet UIView *reverseMatchesView;
#property (strong, nonatomic) IBOutlet UIView *decideLaterView;
- (IBAction)btnMenuClicked:(id)sender;
#end
#implementation MatchesViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Matches";
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:16/255.0 green:97/255.0 blue:61/255.0 alpha:1.0];
[self.navigationController.navigationBar
setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
[self.scrollVw addSubview:self.matchesView];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.matchesView.frame.size.height)];
[self.scrollVw addSubview:self.dailyRecommendationsView];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.dailyRecommendationsView.frame.size.height)];
[self.scrollVw addSubview:self.preferedMatchesView];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.preferedMatchesView.frame.size.height)];
[self.scrollVw addSubview:self.broaderMatchesView];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.broaderMatchesView.frame.size.height)];
[self.scrollVw addSubview:self.twowayMatcheVsiew];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.twowayMatcheVsiew.frame.size.height)];
[self.scrollVw addSubview:self.reverseMatchesView];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.reverseMatchesView.frame.size.height)];
[self.scrollVw addSubview:self.decideLaterView];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.decideLaterView.frame.size.height)];
self.view.backgroundColor = [UIColor clearColor];
self.edgesForExtendedLayout = UIRectEdgeNone;
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
// Do any additional setup after loading the view from its nib.
HMSegmentedControl *segmentedControl1 = [[HMSegmentedControl alloc] initWithSectionTitles:#[#"New Matches", #"Daily Recommendations", #"Prefered Matches", #"Broader Matches", #"2-Way Matches", #"Reverse Matches", #"Decide Later"]];
segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
segmentedControl1.frame = CGRectMake(0, 0, viewWidth, 45);
segmentedControl1.segmentEdgeInset = UIEdgeInsetsMake(0, 5, 0, 5);
segmentedControl1.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
segmentedControl1.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
segmentedControl1.backgroundColor = [UIColor colorWithRed:167/255.0 green:148/255.0 blue:4/255.0 alpha:1.0];
//segmentedControl1.selectionStyle = HMSegmentedControlSelectionStyleBox;
//segmentedControl1.selectedSegmentIndex = HMSegmentedControlNoSegment;
segmentedControl1.selectionIndicatorColor = [UIColor whiteColor];
segmentedControl1.selectionIndicatorHeight = 2.0f;
segmentedControl1.verticalDividerEnabled = YES;
segmentedControl1.verticalDividerColor = [UIColor whiteColor];
segmentedControl1.verticalDividerWidth = 1.0f;
[segmentedControl1 setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
return attString;
}];
[segmentedControl1 addTarget:self action:#selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl1];
[self.navigationController.navigationBar
setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
}
- (void)segmentedControlChangedValue:(UISegmentedControl *)segment
{
switch (segment.selectedSegmentIndex) {
case 0:
self.matchesView.hidden = NO;
self.dailyRecommendationsView.hidden = YES;
self.preferedMatchesView.hidden = YES;
self.broaderMatchesView.hidden = YES;
self.twowayMatcheVsiew.hidden = YES;
self.reverseMatchesView.hidden = YES;
self.decideLaterView.hidden = YES;
break;
case 1:
self.matchesView.hidden = YES;
self.dailyRecommendationsView.hidden = NO;
self.preferedMatchesView.hidden = YES;
self.broaderMatchesView.hidden = YES;
self.twowayMatcheVsiew.hidden = YES;
self.reverseMatchesView.hidden = YES;
self.decideLaterView.hidden = YES;
break;
case 2:
self.matchesView.hidden = YES;
self.dailyRecommendationsView.hidden = YES;
self.preferedMatchesView.hidden = NO;
self.broaderMatchesView.hidden = YES;
self.twowayMatcheVsiew.hidden = YES;
self.reverseMatchesView.hidden = YES;
self.decideLaterView.hidden = YES;
break;
case 3:
self.matchesView.hidden = YES;
self.dailyRecommendationsView.hidden = YES;
self.preferedMatchesView.hidden = YES;
self.broaderMatchesView.hidden = NO;
self.twowayMatcheVsiew.hidden = YES;
self.reverseMatchesView.hidden = YES;
self.decideLaterView.hidden = YES;
break;
case 4:
self.matchesView.hidden = YES;
self.dailyRecommendationsView.hidden = YES;
self.preferedMatchesView.hidden = YES;
self.broaderMatchesView.hidden = YES;
self.twowayMatcheVsiew.hidden = NO;
self.reverseMatchesView.hidden = YES;
self.decideLaterView.hidden = YES;
break;
case 5:
self.matchesView.hidden = YES;
self.dailyRecommendationsView.hidden = YES;
self.preferedMatchesView.hidden = YES;
self.broaderMatchesView.hidden = YES;
self.twowayMatcheVsiew.hidden = YES;
self.reverseMatchesView.hidden = NO;
self.decideLaterView.hidden = YES;
break;
case 6:
self.matchesView.hidden = YES;
self.dailyRecommendationsView.hidden = YES;
self.preferedMatchesView.hidden = YES;
self.broaderMatchesView.hidden = YES;
self.twowayMatcheVsiew.hidden = YES;
self.reverseMatchesView.hidden = YES;
self.decideLaterView.hidden = NO;
break;
default:
break;
}
}
- (void)uisegmentedControlChangedValue:(UISegmentedControl *)segmentedControl {
NSLog(#"Selected index %ld", (long)segmentedControl.selectedSegmentIndex);
}
Does your method segmentedControlChangedValue gets called ?
Try to add a breakpoint on it and to track segment.selectedSegmentIndex value. I think the problem comes from there.
As HMSegmentedControl is not a subclass of UISegmentedControl but of UIControl, the method should be :
- (void)segmentedControlChangedValue:(HMSegmentedControl *)segment
instead of :
- (void)segmentedControlChangedValue:(UISegmentedControl *)segment
Hope this helps.
[segmentedControl1 addTarget:self action:#selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
The event is not correct.
Try to do following code
[segmentedControl1 addTarget:self action:#selector(segmentedControlChangedValue:) forControlEvents:UIControlEventTouchUpInside];
Reinstalling your app.

How to set UIfields using masonry in ios

Hi i am beginner for Auto-Layouts and i am inserting some fields on my UIScrollView they are UILabels and UIButtons and UITextfields using Masonry but i am very ignore about Masonry concept please help me.
I want screen exactly like below image using Masonry concept and i have started some code for doing this requirement but middle of moments i was strucked to do this requirement please help me how to full fill my requirement
my code:-
#import "ViewController.h"
#import "Masonry.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollView];
UITextField * textfield1 = [[UITextField alloc]init];
textfield1.translatesAutoresizingMaskIntoConstraints = NO;
textfield1.backgroundColor = [UIColor blackColor];
[scrollView addSubview:textfield1];
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top).with.offset(padding.top);
make.left.equalTo(self.view.mas_left).with.offset(padding.left);
make.bottom.equalTo(self.view.mas_bottom).with.offset(-padding.bottom);
make.right.equalTo(self.view.mas_right).with.offset(-padding.right);
}];
UIEdgeInsets padding1 = UIEdgeInsetsMake(10, 100, 10, 0);
[textfield1 mas_makeConstraints:^(MASConstraintMaker *make1){
make1.top.equalTo(scrollView.mas_top).width.offset(padding1.top);
make1.left.equalTo(scrollView.mas_left).with.offset(padding1.left);
make1.height.equalTo(scrollView.mas_height).with.offset(30);
make1.right.equalTo(scrollView.mas_right).with.offset(-padding1.right);
}];
}
#end
Here is an example on how to make it work. I did not do all labels but enough to give you an idea how this should be implemented:
#interface ViewController ()
#property (nonatomic) UIScrollView *scrollView;
#property (nonatomic) UILabel *label1;
#property (nonatomic) UITextField *textField1;
#property (nonatomic) UITextField *textField2;
#property (nonatomic) UITextField *textField3;
#property (nonatomic) UIButton *leftButton;
#property (nonatomic) UIButton *rightButton;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
self.scrollView = [[UIScrollView alloc] init];
self.label1 = [[UILabel alloc] init];
self.textField1 = [[UITextField alloc] init];
self.textField2 = [[UITextField alloc] init];
self.textField3 = [[UITextField alloc] init];
self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.label1.text = #"Label 1";
self.leftButton.backgroundColor = [UIColor greenColor];
[self.leftButton setTitle:#"Left Button" forState:UIControlStateNormal];
self.rightButton.backgroundColor = [UIColor greenColor];
[self.rightButton setTitle:#"Right Button" forState:UIControlStateNormal];
self.textField1.placeholder = #"TextField 1";
self.textField2.placeholder = #"TextField 2";
self.textField3.placeholder = #"TextField 3";
self.textField1.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
self.textField2.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
self.textField3.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
[self.view addSubview:self.scrollView];
[self.scrollView addSubview:self.label1];
[self.scrollView addSubview:self.textField1];
[self.scrollView addSubview:self.textField2];
[self.scrollView addSubview:self.textField3];
[self.scrollView addSubview:self.leftButton];
[self.scrollView addSubview:self.rightButton];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(30, 10, 10, 10));
}];
[self.label1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(#0);
make.width.equalTo(self.scrollView);
}];
[self.textField1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.label1.mas_bottom);
make.left.right.equalTo(#0);
make.height.equalTo(#30);
}];
[self.textField2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.textField1.mas_bottom).offset(20);
make.left.equalTo(#0);
make.height.equalTo(self.textField1);
}];
[self.textField3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.textField2);
make.left.equalTo(self.textField2.mas_right).offset(20);
make.right.equalTo(#0);
make.width.equalTo(self.textField2).multipliedBy(0.7);
make.height.equalTo(self.textField1);
}];
[self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.textField2.mas_bottom).offset(40);
make.left.equalTo(#0);
make.bottom.equalTo(#0);
}];
[self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.leftButton);
make.right.equalTo(#0);
make.bottom.equalTo(#0);
make.width.equalTo(self.leftButton);
}];
}
This is what it looks like:
If you want to know a bit more about UIScrollViews and AutoLayout, here is a short blog post I wrote about this topic: UIScrollView and Auto Layout

move subview when keyboard show up

i am trying to show comments list by using subview. I added subview to self.view, and i added a tableview to it in order to show comments list. now i want to enable user to add comment, i tried to add another subview to the first one with text field and button in the bottom of the screen, but now i want to move this view up when the keyboard show up and then back to bottom when submitting the comment, i tried to change commentView frame when textfieldBeginEditing but it do not change !! my way do not work, do you have any idea to do what i want ?
thanks
this is what i am doing, the view called myview is what i want to move up:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
// notification button
myView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth,screenHeight)];
[myView setBackgroundColor:[UIColor whiteColor]];
[myView setTag:2013];
commentView = [[UIView alloc] initWithFrame:CGRectMake(0,screenHeight-70,screenWidth,70)];
[commentView setBackgroundColor:[UIColor redColor]];
// [commentView viewWithTag:2031];
table = [[UITableView alloc] initWithFrame:CGRectMake(0,50, screenWidth, screenHeight-100)];;
table.dataSource=self;
table.delegate=self;
UILabel *currentdate = [[UILabel alloc] initWithFrame:CGRectMake(0,10,screenWidth-40,50)];
currentdate.backgroundColor = [UIColor clearColor];
[currentdate setTextColor: [UIColor blueColor]];
[currentdate setText:#"Comments"];
currentdate.textAlignment= UITextAlignmentCenter;
currentdate.font = [UIFont fontWithName:#"Helvetica" size:20.0];
commentFeild = [[UITextField alloc] initWithFrame:CGRectMake(60,10,screenWidth-60,30)];
commentFeild.placeholder=#"add comment";
commentFeild.backgroundColor = [UIColor whiteColor];
[commentFeild setTextColor: [UIColor blueColor]];
commentFeild.textAlignment= UITextAlignmentRight;
commentFeild.font = [UIFont fontWithName:#"Helvetica" size:15.0];
[commentFeild.layer setCornerRadius:14.0f];
[commentFeild setTag:2113];
//[commentFeild setDelegate:self];
UIButton *doneBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
doneBtn.frame = CGRectMake(screenWidth-45, 10,40, 30);
doneBtn.backgroundColor = [ UIColor clearColor];
[doneBtn setTitle:#"done" forState:UIControlStateNormal];
[doneBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[doneBtn addTarget:self action:#selector(hide) forControlEvents:UIControlEventTouchUpInside];
UIButton *add=[UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(5,10,50,30);
add.backgroundColor = [ UIColor clearColor];
[add setTitle:#"add" forState:UIControlStateNormal];
[add setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[add addTarget:self action:#selector(addComment) forControlEvents:UIControlEventTouchUpInside];
if(![taskObject.status isEqualToString:#"doneTask"])
{
[commentView addSubview:commentFeild];
[commentView addSubview:add];
}
[myView addSubview:doneBtn];
[myView addSubview:currentdate];
[myView addSubview:table];
[myView addSubview:commentView];
[self.view addSubview:myView];
U need to use notifications when keyboard appeared and disappeared
in viewDidLoad method add this
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(keyboardDidDisappear:) name:UIKeyboardDidHideNotification object:nil];
above methods get fired when keyboard appeared and disappeared use this to make adjustments of ur tableview
for exaple
-(void)keyboardDidShow:(NSNotification *)notification
{
NSLog(#"KeyBoard appeared");
NSDictionary *info=[notification userInfo];
NSValue *aValue=[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyBoardRect=[aValue CGRectValue];
keyBoardRect=[self.view convertRect:keyBoardRect fromView:nil];
CGFloat keyBoardTop=keyBoardRect.origin.y; //i am getting the height of the keyboard
self.aTableView.contentInset=UIEdgeInsetsMake(0, 0, keyBoardTop+50, 0); //adjust the height by setting the "contentInset"
}
when keyboard disappeared do like this
-(void)keyboardDidDisappear:(NSNotification *)notification
{
NSLog(#"KeyBoard Disappeared");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
self.addContactTableView.contentInset=UIEdgeInsetsMake(10, 0, 10, 0); //set to normal by setting the "contentInset"
[UIView commitAnimations];
}
a little work around this u may achieve your requirement
hope this helps u :)
I think you should go through the doc
i thought u want to move the tableview, hear is the code of yours modified one
#interface ViewController ( <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
BOOL KeyboardShown;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
KeyboardShown = NO;
// Do any additional setup after loading the view, typically from a nib.
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
// notification button
UIView *myView,*commentView;
myView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth,screenHeight)];
[myView setBackgroundColor:[UIColor whiteColor]];
[myView setTag:2013];
commentView = [[UIView alloc] initWithFrame:CGRectMake(0,screenHeight-70,screenWidth,70)];
[commentView setBackgroundColor:[UIColor redColor]];
// [commentView viewWithTag:2031];
UITableView *table;
table = [[UITableView alloc] initWithFrame:CGRectMake(0,50, screenWidth, screenHeight-100)];;
table.dataSource=self;
table.delegate=self;
table.tag = 12345;
UILabel *currentdate = [[UILabel alloc] initWithFrame:CGRectMake(0,10,screenWidth-40,50)];
currentdate.backgroundColor = [UIColor clearColor];
[currentdate setTextColor: [UIColor blueColor]];
[currentdate setText:#"Comments"];
currentdate.textAlignment= NSTextAlignmentCenter;
currentdate.font = [UIFont fontWithName:#"Helvetica" size:20.0];
UITextField *commentFeild = [[UITextField alloc] initWithFrame:CGRectMake(60,10,screenWidth-60,30)];
commentFeild.placeholder=#"add comment";
commentFeild.backgroundColor = [UIColor whiteColor];
[commentFeild setTextColor: [UIColor blueColor]];
commentFeild.textAlignment= NSTextAlignmentRight;
commentFeild.font = [UIFont fontWithName:#"Helvetica" size:15.0];
[commentFeild.layer setCornerRadius:14.0f];
[commentFeild setTag:2113];
commentFeild.delegate = self;
//[commentFeild setDelegate:self];
UIButton *doneBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
doneBtn.frame = CGRectMake(screenWidth-45, 10,40, 30);
doneBtn.backgroundColor = [ UIColor clearColor];
[doneBtn setTitle:#"done" forState:UIControlStateNormal];
[doneBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[doneBtn addTarget:self action:#selector(hide) forControlEvents:UIControlEventTouchUpInside];
UIButton *add=[UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(5,10,50,30);
add.backgroundColor = [ UIColor clearColor];
[add setTitle:#"add" forState:UIControlStateNormal];
[add setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[add addTarget:self action:#selector(addComment) forControlEvents:UIControlEventTouchUpInside];
// if(![taskObject.status isEqualToString:#"doneTask"])
// {
// [commentView addSubview:commentFeild];
// [commentView addSubview:add];
// }
[myView addSubview:doneBtn];
[myView addSubview:currentdate];
[myView addSubview:table];
[myView addSubview:commentView];
[commentView addSubview:commentFeild];
[self.view addSubview:myView];
}
- (void)viewDidAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(keyboardDidDisappear:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
cell.textLabel.text = #"hello";
return cell;
}
- (void)keyboardDidShow:(NSNotification *)notification
{
//UIView *containerView = [self.view viewWithTag:2013]; comment this and get tableview by TAG
UITableView *tableView = (UITableView *)[self.view viewWithTag:12345];
UIView *commentView = [self.view viewWithTag:1000];//i changed the tag and see u missed the tag for this view in viewDisLoad method check it once
if(!KeyboardShown)
{
KeyboardShown = YES;
NSLog(#"KeyBoard appeared");
NSDictionary *info=[notification userInfo];
NSValue *aValue=[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyBoardRect=[aValue CGRectValue];
keyBoardRect=[self.view convertRect:keyBoardRect fromView:nil];
CGFloat keyBoardTop=keyBoardRect.origin.y; //i am getting the height of the keyboard
CGRect commentViewRect = commentView.frame;
commentViewRect.origin.y = keyBoardTop - 70; //it is the height of comment view
CGRect tableRect = tableView.frame;
tableRect.size.height = tableRect.size.height - keyBoardTop + 77;//adjust the height
[UIView animateWithDuration:0.2 animations:^{
tableView.frame = tableRect;
commentView.frame = commentViewRect;
}];
}
}
- (void)keyboardDidDisappear:(NSNotification *)notification
{
// UIView *containerView = [self.view viewWithTag:2013]; //comment this
//dong same as above , ressetting the same as above
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
UITableView *tableView = (UITableView *)[self.view viewWithTag:12345];
UIView *commentView = [self.view viewWithTag:1000];
if(KeyboardShown)
{
KeyboardShown = NO;
CGRect tableRect =CGRectMake(0,50, screenWidth, screenHeight-100);
CGRect commentViewRect = CGRectMake(0,screenHeight-70,screenWidth,70);
[UIView animateWithDuration:0.2 animations:^{
tableView.frame = tableRect;
commentView.frame = commentViewRect;
}];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
#end

Resources