Following is my code for App Tour using UIScrollView with paging enabled using AutoLayout (Masonry) It works fine in Portrait but when I rotate paging gets disabled so it doesn't work in Landscape. Then it doesn't work in Portrait mode either. Can someone help what's wrong here?
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.scrollView = [UIScrollView new];
self.scrollView.backgroundColor = [UIColor clearColor];
self.scrollView.scrollEnabled = YES;
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
self.scrollView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:self.scrollView];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
UIImageView *esImageview1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CommonResources.bundle/page1"]];
esImageview1.contentMode = UIViewContentModeScaleAspectFit;
esImageview1.backgroundColor = [UIColor clearColor];
esImageview1.userInteractionEnabled = YES;
[self.scrollView addSubview:esImageview1];
[esImageview1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.equalTo(self.scrollView);
make.width.height.equalTo(self.view);
}];
UIImageView *esImageview2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CommonResources.bundle/page2"]];
esImageview2.contentMode = UIViewContentModeScaleAspectFit;
esImageview2.backgroundColor = [UIColor clearColor];
esImageview2.userInteractionEnabled = YES;
[self.scrollView addSubview:esImageview2];
[esImageview2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(esImageview1.mas_right);
make.top.bottom.equalTo(self.scrollView);
make.width.height.equalTo(self.view);
}];
UIImageView *esImageview3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CommonResources.bundle/page3"]];
esImageview3.contentMode = UIViewContentModeScaleAspectFit;
esImageview3.backgroundColor = [UIColor clearColor];
esImageview3.userInteractionEnabled = YES;
[self.scrollView addSubview:esImageview3];
[esImageview3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(esImageview2.mas_right);
make.top.bottom.equalTo(self.scrollView);
make.width.height.equalTo(self.view);
}];
self.pageControl = [UIPageControl new];
self.pageControl.numberOfPages = 3;
self.pageControl.currentPage = 0;
self.pageControl.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.pageControl];
[self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.height.equalTo(#40);
make.top.equalTo(self.view).with.offset(40);
}];
UILabel *titleLabel = [UILabel new];
titleLabel.text = #"App Tour";
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont fontWithName:#"Helvetica-Light" size:16];
titleLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:titleLabel];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.height.equalTo(#40);
make.top.equalTo(self.view).with.offset(20);
}];
UIButton *doneButton = [UIButton new];
[doneButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Light" size:16]];
[doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[doneButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
[doneButton addTarget:self action:#selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
doneButton.backgroundColor = [UIColor clearColor];
[self.view addSubview:doneButton];
[doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view);
make.height.equalTo(#40);
make.width.equalTo(#70);
make.top.equalTo(self.view).with.offset(20);
}];
}
- (void)doneButtonPressed:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*self.pageControl.numberOfPages, self.view.frame.size.height);
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
I'm guessing its getting covered up by another view or something similar to that. Try subclassing the UIScrollView and implementing the following method to check if its responding to your touches at all.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"OMG TOUCHES WORK, NOW WHAT?");
}
You can easily check if it was getting covered up by forcing it to be in front. Call this line of code in the callback for rotation in the UIViewController containing the UIScrollView. It should fix the problem. You may have to subclass the UIView for the controller and put this method in the didLayoutSubviews method instead, not quite sure, depends on your implementation.
[self.view bringSubViewToFront:self.scrollView];
Also check the frame and bounds before and after rotation and see if anything is out of place looking
I just wanted to reset Scrollview's contentsize in didRotateFromInterfaceOrientation delegate. Not sure why though because when I rotate, videDidLayoutSubview already gets called where I am already reseting content size.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*self.pageControl.numberOfPages, self.view.frame.size.height);
}
Related
I have a "scroll to load more" behavior on my UITableView. It starts loading more content when you scroll to the bottom. What I want to achieve is to show a UIActivityIndicatorView at the bottom of the UITableView when service call is in progress, and hide it when new items are added.
What is a good way to solve this problem? I am using Swift but objective-C solutions are also welcome.
I have implemented the same functionality in objective-C
In the cellForRowAtIndexPath
if (indexPath.row == [self.yourArrayData count] - 1)
{
// Call the API
[self loadMoreData:#"all" andPages:[NSString stringWithFormat:#"%ld", (long)pages] AndIsLoadMore:YES];
}
Add loader or design
-(void)addLoaderViewAtBottom {
viewLoader = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-100, self.view.frame.size.width, 50)];
[viewLoader setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:viewLoader];
UIActivityIndicatorView *activityIndicator1 = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator1.center = CGPointMake((SCREEN_SIZE.width/2), 30);
activityIndicator1.alpha = 1.0;
activityIndicator1.hidden = NO;
[viewLoader addSubview:activityIndicator1];
[activityIndicator1 startAnimating];
}
-(void)hideLoaderView {
[UIView animateWithDuration:1
animations:^(void) {
[viewLoader setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 50)];
}];
}
then use this two methods in your API call method. Call addLoaderViewAtBottom when API call starts and call hideLoaderView after getting the responses
Happy coding...
Initialize UIActivityIndicatorView:
#interface MyBulletinVC ()
{
UIActivityIndicatorView *activityLoadMore;
}
-(void)viewDidLoad
{
activityLoadMore = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
}
Check when UITableView is being scrolled and what is the content offset
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
float tblBottomEdge = _tblBulletin.frame.size.height+_tblBulletin.frame.origin.y;
if (bottomEdge <= tblBottomEdge)
{
return;
}
if (bottomEdge >= scrollView.contentSize.height)
{
[self show_bottom_loader];
}
}
If applicable as per content offset then display loader in footer of table:
-(void)show_bottom_loader
{
_tblBulletin.tableFooterView = [self returnLoaderViewWhileFetchingRecords];
CGPoint newContentOffset = CGPointMake(0, [_tblBulletin contentSize].height - _tblBulletin.bounds.size.height);
[_tblBulletin setContentOffset:newContentOffset animated:YES];
[self getNewsRecords];
}
Hide footer when data is loaded:
-(void)hide_bottom_loader
{
_tblBulletin.tableFooterView = nil;
UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
v.backgroundColor = [UIColor clearColor];
[_tblBulletin setTableFooterView:v];
[activityLoadMore stopAnimating];
}
-(UIView*)returnLoaderViewWhileFetchingRecords
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0,_tblBulletin.frame.size.width,44)];
view.backgroundColor =[UIColor clearColor];
UILabel *lblBg =[[UILabel alloc]initWithFrame:CGRectMake(0,1,_tblBulletin.frame.size.width,42)];
lblBg.backgroundColor =[UIColor whiteColor];
[view addSubview:lblBg];
UILabel *lbl =[[UILabel alloc]initWithFrame:CGRectMake(_tblBulletin.frame.size.width/2 - 20,0,_tblBulletin.frame.size.width/2,view.frame.size.height)];
lbl.textAlignment = NSTextAlignmentLeft;
lbl.text = #"Loading...";
lbl.font = [UIFont fontWithName:#"Arial" size:14.0f];
lbl.textColor = [UIColor darkGrayColor];
lbl.backgroundColor = [UIColor clearColor];
lbl.userInteractionEnabled = NO;
[view lbl];
activityLoadMore.frame = CGRectMake(_tblBulletin.frame.size.width/2-35,22,0,0);
[activityLoadMore startAnimating];
[view addSubview:activityLoadMore];
return view;
}
-(void)getNewsRecords
{
//fetch new records and add to your array.
[self hide_bottom_loader];
//reload table.
}
Replace tblBulletin with you table name.
I have setup my UIScrollView with subviews like so using Masonry:
- (void)setupViews {
self.view.backgroundColor = [UIColor Background];
self.scrollView = [UIScrollView new];
self.scrollView.alwaysBounceVertical = YES;
[self.view addSubview:self.scrollView];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
self.contentView = [UIView new];
[self.scrollView addSubview:self.contentView];
self.scrollView.backgroundColor = [UIColor clearColor];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.scrollView);
make.left.and.right.equalTo(self.view);
}];
self.imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.imageButton sd_setImageWithURL:[NSURL URLWithString:self.card.cardImageURL] forState:UIControlStateNormal];
[self.imageButton.imageView setContentMode:UIViewContentModeScaleAspectFill];
[self.contentView addSubview:self.imageButton];
[self.imageButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.and.right.equalTo(self.contentView);
make.height.equalTo(self.imageButton.mas_width).multipliedBy(3/4.0);
}];
self.textField = [UITextField new];
self.textField.backgroundColor = [UIColor whiteColor];
self.textField.font = kFontRegular(14);
self.textField.text = self.card.cardTitle;
[self.contentView addSubview:self.textField];
[self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).with.offset(8);
make.top.equalTo(self.imageButton.mas_bottom).with.offset(8);
make.right.equalTo(self.contentView).with.offset(-8);
make.height.equalTo(#(45));
}];
}
but it will not let me interact with the UITextField. what element here is preventing user interaction?
Your contentView's height will be zero. So it will never get some touch events. You should not use this constraint:
make.edges.equalTo(self.scrollView);
It will not get a correct value. Try to set top and height constraints for your contentView.
make.top.equalTo(#0);
make.height.equalTo(#700);
I'm setting up a subclass of UIView to roll my own UIAlertView style view. I've got everything set up properly with displaying the view, but I'm at a bit of a loss as to how to dismiss the view properly. Specifically, when a user taps on the button in the view, it needs to animate out of the main view. This is the code for the view itself:
+ (void)showCustomAlertWithTitle:(NSString *)titleString andMessage:(NSString *)messageString inView:(UIView *)view andButton1Title: (NSString *)button1Title andButton2Title: (NSString *)button2Title
{
UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
CGRect windowFrame = window.frame;
[view setAlpha:0.5f];
UIColor *buttonColor = [UIColor colorWithRed:0/255.0f green:130/255.0f blue:216/255.0f alpha:1];
UIColor *titleColor = [UIColor colorWithRed:0/255.0f green:153/255.0f blue:102/255.0f alpha:1];
// Shade
UILabel *shadeWindow = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, windowFrame.size.width, windowFrame.size.height)];
shadeWindow.backgroundColor = [UIColor blackColor];
shadeWindow.alpha = 0.50f;
// Define size and origin of alert box
float alertBoxHeight = 225;
float alertBoxWidth = 200;
float alertBoxXorigin = windowFrame.size.width / 2 - (alertBoxWidth / 2);
float alertBoxYorigin = windowFrame.size.height / 2 - (alertBoxHeight / 2);
// Initialize background
UIView *alertBackground = [[UIView alloc] initWithFrame:CGRectMake(alertBoxXorigin, alertBoxYorigin, alertBoxWidth, alertBoxHeight)];
alertBackground.layer.cornerRadius = 5.0f;
[alertBackground.layer setMasksToBounds:YES];
alertBackground.layer.backgroundColor = [UIColor whiteColor].CGColor;
// Title Label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, alertBoxWidth, 40)];
titleLabel.text = titleString;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = titleColor;
titleLabel.layer.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.05f].CGColor;
[titleLabel setFont:[UIFont fontWithName:#"AvenirNext-Bold" size:20.0]];
// Title Divider
UILabel *titleDivider = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, alertBoxWidth, 1.0)];
titleDivider.backgroundColor = [UIColor grayColor];
titleDivider.alpha = 0.5f;
// Alert Message Text
UITextView *alertMessage = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, alertBoxWidth, alertBoxHeight - 90)];
alertMessage.text = messageString;
alertMessage.layer.backgroundColor = [UIColor whiteColor].CGColor;
[alertMessage setFont:[UIFont fontWithName:#"Avenir" size:15.0]];
alertMessage.textAlignment = NSTextAlignmentCenter;
alertMessage.textColor = [UIColor grayColor];
[alertMessage setEditable:NO];
// Button 1
UIButton *button1 = [[UIButton alloc] init];
UIButton *button2 = [[UIButton alloc] init];
[button1 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
if (button2Title == nil)
{
button1.frame = CGRectMake(10, alertBoxHeight - 50, alertBoxWidth - 20, 40);
}
else
{
button1.frame = CGRectMake(10, alertBoxHeight - 50, (alertBoxWidth / 2) - 20, 40);
button2.frame = CGRectMake(alertBoxWidth / 2 + 10, alertBoxHeight - 50, (alertBoxWidth / 2) - 20, 40);
}
button1.layer.backgroundColor = buttonColor.CGColor;
[button1 setTitle:button1Title forState:UIControlStateNormal];
[button1.titleLabel setFont:[UIFont fontWithName:#"AvenirNext-Bold" size:15.0f]];
button1.layer.cornerRadius = 2.5f;
[button1.layer setMasksToBounds:YES];
// Button 2
button2.layer.backgroundColor = buttonColor.CGColor;
[button2 setTitle:button2Title forState:UIControlStateNormal];
[button2.titleLabel setFont:[UIFont fontWithName:#"AvenirNext-Bold" size:15.0f]];
button2.layer.cornerRadius = 2.5f;
[button2.layer setMasksToBounds:YES];
// Bounce Implementation
alertBackground.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^
{
alertBackground.transform = CGAffineTransformIdentity;
}
completion:^(BOOL finished)
{
// do something once the animation finishes, put it here
}];
[window addSubview:view];
[window addSubview:alertBackground];
[view addSubview:shadeWindow];
[view bringSubviewToFront:alertBackground];
[alertBackground addSubview:button1];
[alertBackground addSubview:titleLabel];
[alertBackground addSubview:titleDivider];
[alertBackground addSubview:alertMessage];
[alertBackground addSubview:button2];
[alertBackground bringSubviewToFront:titleDivider];
/* [[[customAlerts sharedInstance] subViewArray] addObject:alertBackground];
[[[customAlerts sharedInstance] subViewArray] addObject:view];
[[[customAlerts sharedInstance] subViewArray] addObject:window];*/
}
When button1 is tapped, for instance, I need to have it animate the view out of the superView and remove it from the stack. I'm not sure how to handle this. Does anyone have any ideas?
To permanently remove a view, I generally use the UIView instance method removeFromSuperview. followed by a line setting the relevant variable to nil:
[myAlertView removeFromSuperview];
myAlertView = nil;
In your case then, I think you need to move the view off the screen by animating the its bounds property, then use the above couple of lines to remove any references to it.
Just found the answer here:
Dismiss view controller from #selector without creating seperate method
Had to download some custom classes but it worked:
[button1 addEventHandler:^(id sender, UIEvent *event)
{
[UIView animateWithDuration:0.25f animations:^{
[alertBackground setAlpha:0.0f];
[shadeWindow setAlpha:0.0f];
[window setAlpha:0.0f];
}];
} forControlEvent:UIControlEventTouchUpInside];
Custom classes can be found here:
https://github.com/ZeR0-Wu/JTTargetActionBlock
I want to load set of objects coming from array in vertical scrollview. I have UIScrollview at xib. Programmatically I added photo image, item name, item tags, comment button and likes label to UIView and added UIView to UIScrollview.
I added likes button to UIScrollview but the button action is not firing. Below is my code.
I think some Frames of subviews are over riding that makes button action (likes_ButtonPressed) not fired.
-(void)createScrollview
{
NSArray *viewsToRemove = [scrollview subviews];
for (UIView *v in viewsToRemove) [v removeFromSuperview];
int x=7;
int y=0;
CGRect screenbounds=[[UIScreen mainScreen] bounds];
for(int i=0;i<[array count];i++)
{
ProfileObjCls *profileObjRef = [self.array objectAtIndex:i];
//NSLog(#"Button Tag is %#",profileObjRef.image_UserLoaded_objInArray);
UIView *mainView=[[UIView alloc]initWithFrame:CGRectMake(x,y,310,400)];
self.photoTagImgView=[[UIImageView alloc]initWithFrame:CGRectMake(x,y+20,294,320)];
self.likesLabel.font=[UIFont fontWithName:#"BodoniStd-BoldItalic" size:13];
self.itemNameLbl=[[UILabel alloc]initWithFrame:CGRectMake(x,y+340,100,30)];
commentsButton=[[UIButton alloc]initWithFrame:CGRectMake(x+155+32, y+340, 40, 40)];
[commentsButton setImage:[UIImage imageNamed:#"comment_icon.png"] forState:UIControlStateNormal];
UIButton *photoLikeButton=[[UIButton alloc]initWithFrame:CGRectMake(x+100, y+340, 40, 40)];
photoLikeButton.tag=i;
[photoLikeButton setImage:[UIImage imageNamed:#"like_img_new.png"] forState:UIControlStateNormal];
[photoLikeButton addTarget:self action:#selector(likes_ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.likesLabel=[[UILabel alloc]initWithFrame:CGRectMake(x+223+32,y+346,310,30)];
if (profileObjRef.photoLikes==0)
{
self.likesLabel.text=#"0 likes";
}
else
{
self.likesLabel.text=[NSString stringWithFormat:#"%# likes",profileObjRef.photoLikes];
}
[self.photoTagImgView setImageWithURL:[NSURL URLWithString:profileObjRef.image_UserLoaded_objInArray]
placeholderImage:[UIImage imageNamed:#"satisfashionplaceholder#568.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
{
if (!error && image)
{
}
}];
self.imagetotweet=profileObjRef.image_UserLoaded_objInArray;
//int y=335;
//first label - PHOTO NAME
self.itemNameLbl.text=profileObjRef.photoname;
self.itemNameLbl.font = [UIFont fontWithName:#"BodoniStd-Poster" size:14.0];//BodoniStd-Bold
self.itemNameLbl.textColor = [UIColor blackColor];
//second Label - PHOTO DESCRIPTION
UIFont *cellFont=[UIFont fontWithName:#"BodoniStd-BoldItalic" size:13.0];
[mainView addSubview:commentsButton];
[mainView addSubview:likesLabel];
[self.scrollview addSubview:photoLikeButton];
[self.scrollview bringSubviewToFront:photoLikeButton];
[mainView addSubview:self.photoTagImgView];
[mainView addSubview:self.itemNameLbl];
[mainView addSubview:self.detailsBgImgView];
[self.scrollview addSubview:likesButton];
[self.scrollview bringSubviewToFront:likesButton];
mainView.userInteractionEnabled=YES;
self.scrollview.backgroundColor=[UIColor clearColor];
self.scrollview.userInteractionEnabled = YES;
/*
self.scrollview.exclusiveTouch = YES;
self.scrollview.canCancelContentTouches = YES;
self.scrollview.delaysContentTouches = YES;
self.view.userInteractionEnabled=YES;*/
//self.scrollview.backgroundColor=[UIColor colorWithRed:212.0f/255.0f green:213.0f/255.0f blue:214.0f/255.0f alpha:1.0f];
[self.scrollview addSubview:mainView];
[self.view addSubview:self.scrollview];
y=y+200;
//x=x+320;
}
self.scrollview.contentSize=CGSizeMake(320, 400*array.count);
//self.scrollview.contentSize=(CGSizeMake(x, 320));
self.scrollview.pagingEnabled=YES;
//[self.scrollview setContentOffset:CGPointMake(320*tagValue, 0)];
[self.scrollview setContentOffset:CGPointMake(0, 400*tagValue)];
[self.view bringSubviewToFront:spinner];
//[self.scrollview setContentOffset:CGPointMake(320*(tagValue-1), 0)];
}
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return ![view isKindOfClass:[UIButton class]];
}
Please suggest any ideas where I went wrong..
Thanks in Advance..
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