UIElements in subclass with uiscrollview are non responsive - ios

I have a class that provides a custom menu for all its subclasses, but since i started using the text field have run to the keyboard problem so i use the normal method and embed the subclass in a resizing uiscrollview. the problem is that the scrollview doesnt cover the menu that belongs to its super class.
The idea that came to mind is to create a scrollview in the super class and inside the scrollview put the menu. I run it and everything seemed to be okay until i couldn't interact with anything on the screen other than the navigation bar and the menu which lead me to believe that maybe the subclass uielements are not subviews of the scrollview.
The problem even expands since in the subclasses I create uielements in the storyboard and in code. My question would be how to make that all uielements created in the subclass and added in self.view become subviews of the superclass' scrollview.
SUPERCLASS
- (void)viewDidLoad
{
[super viewDidLoad];
//_scrollViewMain = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//[self.view addSubview:_scrollViewMain];
_slideButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(slide)];
[self.navigationItem setRightBarButtonItem:_slideButton];
_menuView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height - 44, self.view.bounds.size.width, 44)];
_menuView.backgroundColor = [UIColor colorWithRed:63.0/255.0 green:63.0/255.0 blue:63.0/255.0 alpha:1.0];
[self.view addSubview:_menuView];
//[_scrollViewMain addSubview:_menuView];
UIView *redFrameView = [[UIView alloc] initWithFrame:CGRectMake(0, _menuView.frame.size.height / 2, _menuView.frame.size.width, _menuView.frame.size.height / 2)];
[redFrameView setBackgroundColor:[UIColor colorWithRed:170.0/255.0 green:0.0 blue:0.0 alpha:0.5]];
[_menuView addSubview:redFrameView];
_menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_menuButton setImage:[UIImage imageNamed:#"19-gear.png"] forState:UIControlStateNormal];
[_menuButton addTarget:self action:#selector(menu) forControlEvents:UIControlEventTouchUpInside];
_menuButton.frame = CGRectMake(16.0, 9.0, 26.0, 26.0);
_addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_addButton setImage:[UIImage imageNamed:#"13-plus.png"] forState:UIControlStateNormal];
[_addButton addTarget:self action:#selector(add) forControlEvents:UIControlEventTouchUpInside];
_addButton.frame = CGRectMake(16.0, 15.0, 14.0, 14.0);
_addButton.userInteractionEnabled = NO;
_addButton.alpha = 0.0;
_editButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_editButton setImage:[UIImage imageNamed:#"187-pencil.png"] forState:UIControlStateNormal];
[_editButton addTarget:self action:#selector(edit) forControlEvents:UIControlEventTouchUpInside];
_editButton.frame = CGRectMake(16.0, 15.0, 14.0, 14.0);
_editButton.userInteractionEnabled = NO;
_editButton.alpha = 0.0;
_deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_deleteButton setImage:[UIImage imageNamed:#"11-x.png"] forState:UIControlStateNormal];
[_deleteButton addTarget:self action:#selector(delete) forControlEvents:UIControlEventTouchUpInside];
_deleteButton.frame = CGRectMake(16.0, 15.0, 14.0, 14.0);
_deleteButton.userInteractionEnabled = NO;
_deleteButton.alpha = 0.0;
_downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_downloadButton setImage:[UIImage imageNamed:#"57-download.png"] forState:UIControlStateNormal];
[_downloadButton addTarget:self action:#selector(download) forControlEvents:UIControlEventTouchUpInside];
_downloadButton.frame = CGRectMake(16.0, 15.0, 11.0, 14.0);
_downloadButton.userInteractionEnabled = NO;
_downloadButton.alpha = 0.0;
_uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_uploadButton setImage:[UIImage imageNamed:#"56-cloud.png"] forState:UIControlStateNormal];
[_uploadButton addTarget:self action:#selector(upload) forControlEvents:UIControlEventTouchUpInside];
_uploadButton.frame = CGRectMake(16.0, 15.0, 21.0, 14.0);
_uploadButton.userInteractionEnabled = NO;
_uploadButton.alpha = 0.0;
_shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_shareButton setImage:[UIImage imageNamed:#"124-bullhorn.png"] forState:UIControlStateNormal];
[_shareButton addTarget:self action:#selector(share) forControlEvents:UIControlEventTouchUpInside];
_shareButton.frame = CGRectMake(16.0, 15.0, 21.0, 14.0);
_shareButton.userInteractionEnabled = NO;
_shareButton.alpha = 0.0;
[_menuView addSubview:_addButton];
[_menuView addSubview:_editButton];
[_menuView addSubview:_deleteButton];
[_menuView addSubview:_downloadButton];
[_menuView addSubview:_uploadButton];
[_menuView addSubview:_shareButton];
[_menuView addSubview:_menuButton];
_isMenuVisible = NO;
}
FIRST attempt but the scrolldidn't included the menuview in the superclass
- (void)viewDidLoad
{
[super viewDidLoad];
[self registerForKeyboardNotifications];
self.title = [_exercise valueForKey:kExerciseName];
[super ableAdd];
[super ableDownload];
[super ableShare];
if ([[_exercise valueForKey:kExerciseIsDefault] boolValue]) {
[super ableUpload];
[super ableDelete];
}
_doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
_nameLabel.text = [_exercise valueForKey:kExerciseName];
_nameLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
_categoryLabel.text = [[_exercise valueForKey:kExerciseEType] valueForKey:kExerciseTypeName];
_categoryLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
_gearLabel.text = [[_exercise valueForKey:kGear] valueForKey:kGearName];
_gearLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
_addRecordButton = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 30) / 2, 7, 30, 30)];
[_addRecordButton setImage:[UIImage imageNamed:#"85-trophy.png"] forState:UIControlStateNormal];
[_addRecordButton addTarget:self action:#selector(addRecord) forControlEvents:UIControlEventTouchUpInside];
[_toolView addSubview:_addRecordButton];
_showChartButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 50, 7, 30, 30)];
[_showChartButton setImage:[UIImage imageNamed:#"16-line-chart.png.png"] forState:UIControlStateNormal];
[_showChartButton addTarget:self action:#selector(showChart) forControlEvents:UIControlEventTouchUpInside];
[_toolView addSubview:_showChartButton];
_numberOfPages = 3;
_currentPage = 1;
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 174, 320, self.view.bounds.size.height - 262)];
_scrollView.pagingEnabled = YES;
_scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width * _numberOfPages, _scrollView.bounds.size.height);
_scrollView.delegate = self;
_scrollView.showsHorizontalScrollIndicator = NO;
_textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
_textView.contentSize = CGSizeMake(_scrollView.bounds.size.width, _scrollView.bounds.size.height);
_textView.text = [_exercise valueForKey:kExerciseOverview];
_textView.editable = NO;
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(_scrollView.bounds.size.width * 1, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
_tableView.contentSize = CGSizeMake(_scrollView.bounds.size.width, _scrollView.bounds.size.height);
_tableView.dataSource = self;
_tableView.delegate = self;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleLongPress:)];
longPress.minimumPressDuration = 2.0;
longPress.delegate = (id<UIGestureRecognizerDelegate>)self;
[_tableView addGestureRecognizer:longPress];
_movieView = [[UIWebView alloc] initWithFrame:CGRectMake(_scrollView.bounds.size.width * 2, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
_movieView.opaque = YES;
_movieView.backgroundColor = [UIColor whiteColor];
_movieView.autoresizesSubviews = YES;
[_movieView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[_exercise valueForKey:kExerciseUrlAddress]]]];
[_scrollView addSubview:_textView];
[_scrollView addSubview:_tableView];
[_scrollView addSubview:_movieView];
[_scrollView scrollRectToVisible:CGRectMake(_scrollView.bounds.size.width * 1, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height) animated:NO];
[_scrollViewMain addSubview:_scrollView];
//[self.view addSubview:_scrollView];
_pageControl.numberOfPages = _numberOfPages;
_pageControl.currentPage = _currentPage;
_records = [[NSMutableArray alloc] init];
_records = [[NSArray arrayWithArray:[[_exercise valueForKey:kExerciseERecords] allObjects]] mutableCopy];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:kExerciseRecordRecordDate ascending:NO];
[_records sortUsingDescriptors:[NSArray arrayWithObject:sort]];
}
- (void)viewDidDisappear:(BOOL)animated
{
if (self.editing) {
[super setPan];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[_tableView setEditing:editing animated:animated];
[super setPan];
if (self.navigationItem.rightBarButtonItem == nil) {
self.navigationItem.rightBarButtonItem = _doneButton;
}
_addRecordButton.enabled = !editing;
_showChartButton.enabled = !editing;
if (![[_exercise valueForKey:kExerciseIsDefault] boolValue]) {
[super ableUpload];
[super ableDelete];
_textView.editable = editing;
}
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_scrollViewMain.contentInset = contentInsets;
_scrollViewMain.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, _activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, _activeField.frame.origin.y-kbSize.height);
[_scrollViewMain setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
_scrollViewMain.contentInset = contentInsets;
_scrollViewMain.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
_activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
_activeField = nil;
}

Related

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);
}

How to setting NavigationBar's barButtonItems

Now,I found the problem maybe from my custom button--DJExchangeImageTitleButton.This is the code:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)setFrame:(CGRect)frame
{
frame.size.width += DJMargin;
[super setFrame:frame];
}
- (void)layoutSubviews
{
[super layoutSubviews];
// 如果仅仅是调整按钮内部titleLabel和imageView的位置,那么在layoutSubviews中单独设置位置即可
// 1.计算titleLabel的frame
self.titleLabel.x = self.imageView.x;
// 2.计算imageView的frame
self.imageView.x = CGRectGetMaxX(self.titleLabel.frame) + DJMargin;
}
- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
[super setTitle:title forState:state];
// 只要修改了文字,就让按钮重新计算自己的尺寸
[self sizeToFit];
}
- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
[super setImage:image forState:state];
// 只要修改了图片,就让按钮重新计算自己的尺寸
[self sizeToFit];
}
-(void)setHighlighted:(BOOL)highlighted{};
Who can tell me what's wrong with my customed button?I will appreciate a lot.
When I set navigationBar's left,right and titleView,and it's normal;
However,every times,I push to another controller,and I pop back,the left and right barButtonItems become longer and longer,the titleView is shorter and shorter.
This is my code:
- (void)viewDidLoad {
[super viewDidLoad];
[self setupNaviBar];}
-(void)setupNaviBar{
//leftButton
DJExchangeImageTitleButton *titleBtn = [[DJExchangeImageTitleButton alloc]init];
[titleBtn setImage:[UIImage imageNamed:#"homgpage_navi_left"] forState:UIControlStateNormal];
[titleBtn setTitle:#"深圳" forState:UIControlStateNormal];
titleBtn.backgroundColor = [UIColor redColor];
titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
titleBtn.frame = CGRectMake(0, 0, 13, 25);
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:titleBtn];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = -10;
self.navigationItem.leftBarButtonItems = #[negativeSpacer,leftItem];
//searchBar
UIButton *btn = [[UIButton alloc]init];
btn.backgroundColor = [UIColor whiteColor];
btn.frame = CGRectMake(0, 0, 250, 25);
btn.layer.cornerRadius = 3;
[btn setImage:[UIImage imageNamed:#"search_icon"] forState:UIControlStateNormal];
[btn setTitle:#"请输入要搜索的职位名称" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:13.0];
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btn.imageEdgeInsets =UIEdgeInsetsMake(0, 10, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
[btn addTarget:self action:#selector(goSearch) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = btn;
//rightButton
DJExchangeImageTitleButton *notiBtn = [[DJExchangeImageTitleButton alloc]init];
notiBtn.backgroundColor = [UIColor redColor];
[notiBtn setTitle:#"通知" forState:UIControlStateNormal];
[notiBtn setImage:[UIImage imageNamed:#"homgpage_navi_right"] forState:UIControlStateNormal];
notiBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
notiBtn.frame = CGRectMake(0, 0, 13, 25);
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithCustomView:notiBtn];
self.navigationItem.rightBarButtonItem = right;
}
How can I solve this problem?Sorry I can't post a image to show my question.

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

UIScrollView add zoom for the self.view

I need zooming all objects in the view, but lose the position of the objects (UIButton)
- (void)viewDidLoad
{
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 460);
scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
scrollView.delegate = self;
scrollView.minimumZoomScale = 1;
scrollView.maximumZoomScale = 4.0;
[self.view addSubview:scrollView];
CGSize scrollViewContentSize = CGSizeMake(640, 800);
[scrollView setContentSize:scrollViewContentSize];
NSMutableArray *datos = [[NSMutableArray alloc]initWithObjects:#"ob1",#"ob2",#"ob3",#"ob4", nil];
for (int i = 0; i < [datos count]; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(100, 100*i, 70, 70);
button.tag = i;
[button setTitle: #"Line1" forState: UIControlStateNormal];
button.hidden= NO;
[scrollView addSubview:button];
}
}
-(void)aMethod{
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return scrollView.superview;
}
You should setup a subview container and pass that as the viewForZoomingInScrollView.
The subview should contain your buttons:
self.buttonSubview = [[UIView alloc] initWithFrame:...];
...
[buttonSubview addSubview:button];
...
[scrollView addSubview:buttonSubview];
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.buttonSubview;
}

uibutton is not getting displayed

I am trying to add uibutton inside the imageview.
But it was not displayed the button.
Even i tried adding it to the uiscrollview and also for self.view.
But nothing were displayed the uibutton
Pls let me know what is the problem
const CGFloat HEIGHT = 1024.0;
const CGFloat WIDTH = 768.0;
#define myViewPortrait CGRectMake(0.0f, 0.0f, 768.0f,1024.0f)
#define myViewLandSacpe CGRectMake(0.0f, 0.0f, 1024.0f,768.0f)
#define kAnimationKey #"animationKey"
-(void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [myScrollView subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (self.view.frame.size.width);
}
}
[myScrollView setContentSize:CGSizeMake((myImagesCount * self.view.frame.size.width), [myScrollView bounds].size.height)];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
myScrollView = [[UIScrollView alloc] initWithFrame:
CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:myScrollView];
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightRecognizer.numberOfTouchesRequired = 1;
[rightRecognizer setDelegate:self];
[myScrollView addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
leftRecognizer.numberOfTouchesRequired = 1;
[leftRecognizer setDelegate:self];
[myScrollView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
[myScrollView setBackgroundColor:[UIColor blackColor]];
[myScrollView setCanCancelContentTouches:NO];
myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
myScrollView.clipsToBounds = YES;
myScrollView.scrollEnabled = YES;
myScrollView.pagingEnabled = YES;
myScrollView.delegate = self;
myImagesCount = 5;
myScrollView.showsHorizontalScrollIndicator=NO;
myScrollView.showsVerticalScrollIndicator=NO;
for (int i = 1; i <= myImagesCount; i++)
{
NSString *imageName = [NSString stringWithFormat:#"screen-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = myScrollView.frame.size.height;
NSLog(#"%d -----",self.view.frame.size.width);
rect.size.width = myScrollView.frame.size.width;
imageView.frame = rect;
imageView.tag = i;
[myScrollView addSubview:imageView];
[imageView release];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
button.backgroundColor = [UIColor blackColor];
[self.view addSubview:button];
[self layoutScrollImages];
[super viewDidLoad];
}
Based off your question, you'd like to add the UIButton as a subview to the UIImageView - I'm guessing in the for loop? Immediate problem I see is that the actual button is being generated outside of the for loop. I think your for loop is intended to look like this:
for (int i = 1; i <= myImagesCount; i++)
{
NSString *imageName = [NSString stringWithFormat:#"screen-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
button.backgroundColor = [UIColor blackColor];
CGRect rect = imageView.frame;
rect.size.height = myScrollView.frame.size.height;
NSLog(#"%d -----",self.view.frame.size.width);
rect.size.width = myScrollView.frame.size.width;
imageView.frame = rect;
imageView.tag = i;
[imageView addSubview:button];
[myScrollView addSubview:imageView];
[imageView release];
}
Try this
Declare button globally and set
[self layoutScrollImages];
[self.view bringSubViewToFront:button];

Resources