UIScrollView not scrolling programmatically in iOS 6 & 7 both - ios

For testing purpose I have create project where I have two scrollview. First scrollview have images that I inserted manually and in second scrollview I have list of buttons (as menu).
I have button as right and left to scroll UIScrollView programmatically.
After clicking this button I was moving scrollview by setting content size.
myCounter.text = [NSString stringWithFormat:#"%d", [myCounter.text intValue] - 1];
CGRect page = CGRectMake(320*([myCounter.text intValue] ), 230, 320, 150);
[myScrollView scrollRectToVisible:page animated:YES];
CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 20, 320, 200);
[firstScrollView scrollRectToVisible:page2 animated:YES];
The problem I am facing is only first scroll view is getting scrolled but not the second one.
I am really confused why this is happening.
I am also attaching same project at dropbox as code is little more.
Project at dropbox
Full Code if some one don't want to download project
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;
#property (weak, nonatomic) IBOutlet UILabel *myCounter;
#property (weak, nonatomic) IBOutlet UIScrollView *firstScrollView;
- (IBAction)goToLeft:(id)sender;
- (IBAction)goToRight:(id)sender;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize myScrollView, myCounter, firstScrollView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
firstScrollView.contentSize = CGSizeMake(800, 200);
myCounter.text = #"0";
}
- (void) viewDidAppear:(BOOL)animated {
for (int j = 0; j < 2; j++) {
NSString *bname = #"";
int x = 20;
for (int i = 0; i < 8; i++) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 66, 75)];
UIButton *rightArrowButton;
UIButton *leftArrowButton;
leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(x-38, 49, 26, 52)];
if (i==2) {
rightArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(294, 49, 26, 52)];
leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(1, 49, 26, 52)];
} else if (i==5) {
rightArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(614, 49, 26, 52)];
leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(320, 49, 26, 52)];
} else if (i==7) {
rightArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(934, 49, 26, 52)];
leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(640, 49, 26, 52)];
} else {
rightArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(x+62, 49, 26, 52)];
leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(x-38, 49, 26, 52)];
}
bname = #"";
if (i==0) {
bname = #"doctors_icon.png";
rightArrowButton.hidden = YES;
leftArrowButton.hidden = YES;
}
if (i==1) {
bname = #"all-offers_icon.png";
rightArrowButton.hidden = YES;
leftArrowButton.hidden = YES;
}
if (i==2) {
bname = #"hospitals_icon.png";
rightArrowButton.hidden = NO;
leftArrowButton.hidden = YES;
}
if (i==3) {
bname = #"ads_icon.png";
rightArrowButton.hidden = YES;
leftArrowButton.hidden = YES;
}
if (i==4) {
bname = #"alternative_icon.png";
rightArrowButton.hidden = YES;
leftArrowButton.hidden = YES;
}
if (i==5) {
bname = #"pharmacy_icon.png";
rightArrowButton.hidden = NO;
leftArrowButton.hidden = NO;
}
if (i==6) {
bname = #"equ_icon.png";
rightArrowButton.hidden = YES;
leftArrowButton.hidden = YES;
}
if (i==7) {
bname = #"supplies_icon.png";
rightArrowButton.hidden = YES;
leftArrowButton.hidden = NO;
}
UIImage *btnImage = [UIImage imageNamed:bname];
[button setImage:btnImage forState:UIControlStateNormal];
[button setTag:(i+1)];
[myScrollView addSubview:button];
btnImage = [UIImage imageNamed:#"r_arrow.png"];
[rightArrowButton setImage:btnImage forState:UIControlStateNormal];
[rightArrowButton setTag:998];
[rightArrowButton addTarget:self action:#selector(goToRight:) forControlEvents:UIControlEventTouchUpInside];
[myScrollView addSubview:rightArrowButton];
btnImage = [UIImage imageNamed:#"l_arrow.png"];
[leftArrowButton setImage:btnImage forState:UIControlStateNormal];
[leftArrowButton setTag:999];
[leftArrowButton addTarget:self action:#selector(goToLeft:) forControlEvents:UIControlEventTouchUpInside];
[myScrollView addSubview:leftArrowButton];
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(x, 76, 66, 75)];
if (i==0) {
bname = #"medical_icon.png";
}
if (i==1) {
bname = #"dental_icon.png";
}
if (i==2) {
bname = #"beauty_icon.png";
}
if (i==3) {
bname = #"labs_icon.png";
}
if (i==4) {
bname = #"magazine_icon.png";
}
if (i==5) {
bname = #"news_icon.png";
}
if (i<=5) {
btnImage = [UIImage imageNamed:bname];
[button2 setImage:btnImage forState:UIControlStateNormal];
[button2 setTag:(i+8+1)];
[myScrollView addSubview:button2];
}
x += button.frame.size.width + 40;
}
}
// myScrollView.delegate = self;
myScrollView.contentSize = CGSizeMake(320*3, 150);
myScrollView.backgroundColor = [UIColor clearColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)goToLeft:(id)sender {
myCounter.text = [NSString stringWithFormat:#"%d", [myCounter.text intValue] - 1];
CGRect page = CGRectMake(320*([myCounter.text intValue] ), 230, 320, 150);
[myScrollView scrollRectToVisible:page animated:YES];
CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 20, 320, 200);
[firstScrollView scrollRectToVisible:page2 animated:YES];
NSLog(#"myCounter.text==%d", 320*([myCounter.text intValue]));
}
- (IBAction)goToRight:(id)sender {
myCounter.text = [NSString stringWithFormat:#"%d", [myCounter.text intValue] + 1];
CGRect page = CGRectMake(320*([myCounter.text intValue] ), 230, 320, 150);
[myScrollView scrollRectToVisible:page animated:YES];
CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 20, 320, 200);
[firstScrollView scrollRectToVisible:page2 animated:YES];
NSLog(#"myCounter.text==%d", 320*([myCounter.text intValue]));
}
#end

I have downloaded your code and modify the code below, your "page.origin.y is out of the contentSize.height"
- (IBAction)goToLeft:(id)sender {
myCounter.text = [NSString stringWithFormat:#"%d", [myCounter.text intValue] - 1];
CGRect page = CGRectMake(320*([myCounter.text intValue] ), 0, 320, 150);
[myScrollView scrollRectToVisible:page animated:YES];
CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 0, 320, 200);
[firstScrollView scrollRectToVisible:page2 animated:YES];
NSLog(#"myCounter.text==%d", 320*([myCounter.text intValue]));
}
- (IBAction)goToRight:(id)sender {
myCounter.text = [NSString stringWithFormat:#"%d", [myCounter.text intValue] + 1];
CGRect page = CGRectMake(320*([myCounter.text intValue] ), 0, 320, 150);
[myScrollView scrollRectToVisible:page animated:YES];
CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 0, 320, 200);
[firstScrollView scrollRectToVisible:page2 animated:YES];
NSLog(#"myCounter.text==%d", 320*([myCounter.text intValue]));
}

Hey mate please set the contentSize of the scrollview in your viewDidLoad method.
[myScrollView setContentSize:CGSizeMake(320*3,200)];
Thanks and have a blessed day

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 open and close tableView on a button click?

2I'm working on dropDowns, i'm creating buttons and dropdown tableView dynamically. When i click first button last tableView was opened. But i want to open specific tableView (at a time i want to open only one dropdown).
float y = 0;
float x = (self.answersView.frame.size.width/2)+175;
float Iy = 0;
float Ix = 675;
float Tx = (self.answersView.frame.size.width/2)+175;
float Ty = 0;
for (int i=0; i<self.radioBtnTagArr.count; i++) {
self.dropdownBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.dropdownBtn.frame = CGRectMake(x, y+30, (self.answersView.frame.size.width/2)-200, 50);
[self.dropdownBtn setTitle:#"0" forState:UIControlStateNormal];
self.dropdownBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.dropdownBtn setTitleColor:[UIColor colorWithRed:24/255.0 green:56/255.0 blue:131/255.0 alpha:1] forState:UIControlStateNormal];
self.dropdownBtn.titleLabel.font = [UIFont systemFontOfSize:25];
[self.dropdownBtn addTarget:self action:#selector(dropdownBtnTapMethod:) forControlEvents:UIControlEventTouchUpInside];
self.dropdownBtn.backgroundColor = [UIColor lightGrayColor];
[self.scrollView addSubview:self.dropdownBtn];
self.dropdownBtn.tag = [[self.btnTagArr objectAtIndex:i] intValue];
y = y+70;
self.dropdownImageView = [[UIImageView alloc]initWithFrame:CGRectMake(Ix, Iy+50, 20, 10)];
self.dropdownImageView.image = [UIImage imageNamed:#"dropdown.png"];
[self.answersView addSubview:self.dropdownImageView];
Iy = Iy+70;
self.dropdownTableView = [[UITableView alloc]initWithFrame:CGRectMake(Tx, Ty+80, (self.answersView.frame.size.width/2)-200, 50) style:UITableViewStylePlain];
self.content = #[ #"Monday", #"Tuesday", #"Wednesday",#"Thursday",#"Friday",#"Saturday",#"Sunday"];
self.dropdownTableView.delegate = self;
self.dropdownTableView.dataSource = self;
[self.answersView addSubview:self.dropdownTableView];
Ty = Ty+70;
self.dropdownTableView.hidden = YES;
}
//The event handling method
- (void)dropdownBtnTapMethod:(UIButton *) sender {
if (self.dropdownTableView.hidden == YES) {
self.dropdownTableView.hidden = NO;
} else {
self.dropdownTableView.hidden = YES;
}
}
And how to set table view height dynamically.
Try this :
NSMutableArray *arrTableview; // make global
In ViewDidLoad
arrTableview = [NSMutableArray New];
Change in following method
for (int i=0; i<self.radioBtnTagArr.count; i++) {
self.dropdownBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.dropdownBtn.tag = i; // add this line
self.dropdownBtn.frame = CGRectMake(x, y+30, (self.answersView.frame.size.width/2)-200, 50);
[self.dropdownBtn setTitle:#"0" forState:UIControlStateNormal];
self.dropdownBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.dropdownBtn setTitleColor:[UIColor colorWithRed:24/255.0 green:56/255.0 blue:131/255.0 alpha:1] forState:UIControlStateNormal];
self.dropdownBtn.titleLabel.font = [UIFont systemFontOfSize:25];
[self.dropdownBtn addTarget:self action:#selector(dropdownBtnTapMethod:) forControlEvents:UIControlEventTouchUpInside];
self.dropdownBtn.backgroundColor = [UIColor lightGrayColor];
[self.scrollView addSubview:self.dropdownBtn];
//self.dropdownBtn.tag = [[self.btnTagArr objectAtIndex:i] intValue]; // This line commented.
y = y+70;
self.dropdownImageView = [[UIImageView alloc]initWithFrame:CGRectMake(Ix, Iy+50, 20, 10)];
self.dropdownImageView.image = [UIImage imageNamed:#"dropdown.png"];
[self.answersView addSubview:self.dropdownImageView];
Iy = Iy+70;
self.dropdownTableView = [[UITableView alloc]initWithFrame:CGRectMake(Tx, Ty+80, (self.answersView.frame.size.width/2)-200, 50) style:UITableViewStylePlain];
self.content = #[ #"Monday", #"Tuesday", #"Wednesday",#"Thursday",#"Friday",#"Saturday",#"Sunday"];
self.dropdownTableView.delegate = self;
self.dropdownTableView.dataSource = self;
[self.answersView addSubview:self.dropdownTableView];
[arrTableview addObject :self.dropdownTableView];
Ty = Ty+70;
self.dropdownTableView.hidden = YES;
}
Selector
- (void)dropdownBtnTapMethod:(UIButton *) sender {
UITableView *tableView =[arrTableview objectAtIndex:sender.tag]
if (tableView.hidden == YES) {
tableView.hidden = NO;
} else {
tableView.hidden = YES;
}
}

UITextField will not become first responder when touched, none of the usual mistakes apply

I have a UITextField that will not become the first responder when tapped. I can assign it to become first responder, which works find. But if it resigns first responder status and I try and tap it or tab back to it to make it become the first responder again, nothing happens. It appears as if the touch is being trapped somewhere, and yet I can't find anything in my code that could be causing that to happen. I've checked the usual suspects:
If the textfield the top view
is the textfield within the bounds of it's superview
is the textfield userEnabled.
I've also rewritten the code in several different ways, to no avail.
Can anyone help me with this problem. The textField at issue is the field titled answerTextField in the method createOneDoubleViewAtOrigin.
The relevant code is below.
-(instancetype)initForProblem:(NSString *)problem{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
self = [super init];
if (self) {
[self parseBasicFractionProblem:problem];
if (_problemType == fractDoubleWithPic) {
NSLog(#"placed the fract views");
UIView *firstProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(26, 30) withNumerator:_numerator1 denominator:_denominator1 forViewNumber:0];
UIView *secondProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(342,30) withNumerator:_numerator2 denominator:_denominator2 forViewNumber:1];
[self addSubview:firstProblemView];
[self addSubview:secondProblemView];
[self bringSubviewToFront:firstProblemView];
[self bringSubviewToFront:secondProblemView];
}
else if (_problemType == fractDoubleNoPicAns||_problemType == fractDoubleNoPicExtendedAns ){
}
}
self.tag = 800;
self.backgroundColor = [UIColor redColor];
NSLog(#"made to end");
return self;
}
-(UIView *)createOneDoubleViewAtOrigin:(CGPoint)viewOrigin withNumerator:(NSInteger)numerator denominator:(NSInteger)denominator forViewNumber:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UIView *containerView = [[UIView alloc] initWithFrame: CGRectMake(viewOrigin.x,viewOrigin.y, 310, 263)];
containerView.backgroundColor = [UIColor colorWithRed:178.0/255.0 green:222.0/255.0 blue:80.0/255.0 alpha:1.0];
containerView.layer.cornerRadius = 5.0;
UILabel *numeratorView = [self createSubview:CGRectMake(66, 23, 59, 47) text:[NSString stringWithFormat:#"%ld",(long)numerator] inView:containerView];
UILabel *divisorView = [self createSubview:CGRectMake(66, 40, 59, 47) text:#"___" inView:containerView];
UILabel *denominatorView = [self createSubview:CGRectMake(72, 82, 47, 47) text:[NSString stringWithFormat:#"%ld",(long)denominator] inView:containerView];
UILabel *equals = [self createSubview:CGRectMake(125, 50, 47, 47) text:#"=" inView:containerView];
/*
FFractSupportedTextField *answerField = [self createAnswerField:CGRectMake(173,50,82,47)];
*/
UITextField *answerTextField = [[UITextField alloc] initWithFrame:CGRectMake(173,50,82,47)];
//Inside
answerTextField.font = [UIFont fontWithName:#"Helvetica" size:30.0];
answerTextField.textAlignment = NSTextAlignmentCenter;
answerTextField.placeholder = #"?";
//border
answerTextField.layer.borderWidth = 1;
answerTextField.layer.borderColor = [[UIColor blackColor] CGColor];
answerTextField.layer.cornerRadius = 5.0;
answerTextField.userInteractionEnabled = YES;
[answerTextField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
containerView.tag = 820 + 6*viewNumber;
numeratorView.tag = 821 + 6*viewNumber;
divisorView.tag = 822 + 6*viewNumber;
denominatorView.tag = 823 + 6*viewNumber;
equals.tag = 824 + 6*viewNumber;
answerTextField.tag = 801 + viewNumber;
UIView *pictureView = [self createFractPictureForNumerator:numerator denominator:denominator number:viewNumber];
pictureView.tag = 825 + 6*viewNumber;
if (viewNumber == 0){
_answerTextField1 = answerTextField;
[containerView addSubview:_answerTextField1];
[containerView bringSubviewToFront:_answerTextField1];
_pictureView1 = pictureView;
[containerView addSubview:_pictureView1];
[_answerTextField1 becomeFirstResponder];
} else if (viewNumber == 1) {
_answerTextField2 = answerTextField;
[containerView addSubview:_answerTextField2];
[containerView bringSubviewToFront:_answerTextField2];
_pictureView2 = pictureView;
[containerView addSubview:_pictureView2];
}
return containerView;
}
-(UILabel *)createSubview:(CGRect)frame text:(NSString *)text inView:(UIView *)containerView{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UILabel *labelView = [[UILabel alloc] initWithFrame:frame];
labelView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
labelView.textAlignment = NSTextAlignmentCenter;
labelView.text = text;
[containerView addSubview:labelView];
return labelView;
}
-(FFractSupportedTextField *)createAnswerField:(CGRect)frame{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
FFractSupportedTextField *fieldView = [[FFractSupportedTextField alloc] initWithFrame:frame];
//Inside
fieldView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
fieldView.textAlignment = NSTextAlignmentCenter;
fieldView.placeholder = #"?";
//border
fieldView.layer.borderWidth = 1;
fieldView.layer.borderColor = [[UIColor blackColor] CGColor];
fieldView.layer.cornerRadius = 5.0;
fieldView.userInteractionEnabled = YES;
return fieldView;
}
-(UIView *)createFractPictureForNumerator:(NSInteger)numerator denominator:(NSInteger)denominator number:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
NSLog(#"numerator:%ld denominator:%ld",(long)numerator,(long)denominator);
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(33, 165, 256, 78)];
containerView.backgroundColor = [UIColor whiteColor];
containerView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
containerView.layer.borderWidth = 1.0;
containerView.layer.cornerRadius = 3.0;
NSInteger smallViewCount = denominator;
if (denominator == 0) {
smallViewCount = 1;
}
float smallWidth = 245.0/smallViewCount;
for (int n = 0; n < smallViewCount; n++) {
NSLog(#"count %d",n);
UILabel *smallLabel = [[UILabel alloc] initWithFrame:CGRectMake(8 + n*smallWidth, 8, smallWidth - 5, 29)];
smallLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
smallLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
[smallLabel setAdjustsFontSizeToFitWidth:YES];
smallLabel.textAlignment = NSTextAlignmentCenter;
smallLabel.layer.cornerRadius = 3.0;
smallLabel.tag = 830+n + viewNumber*10;
[containerView addSubview:smallLabel];
}
UILabel *largeLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 41, 240, 29)];
largeLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
largeLabel.text = [NSString stringWithFormat:#"= %ld",(long)numerator];
largeLabel.textAlignment = NSTextAlignmentCenter;
largeLabel.layer.cornerRadius = 3.0;
[containerView addSubview:largeLabel];
NSLog(#"end of createFractPictFor..");
return containerView;
}

UIScrollView set its contentOffset default?

I have a scroll view which consists of many of views. These views are placed in the view according to their dynamic size. There is also a delete button on each view which deletes the view from the scrollview, and replace the others accordingly.
But I have a problem, when I delete something scrollview does not stay where I deleted. One row up or down.
This is my deletion code:
-(void) redrawTheList:(id)sender :(NSString*) except{
int tempo = counterRow;
NSMutableDictionary *doctorTemp = [[NSMutableDictionary alloc]initWithDictionary:doctorAddition copyItems:YES];
for(int i = 0; i<=tempo;i++)
{
NSString *dictTempKey = [NSString stringWithFormat:#"row%d",i];
NSMutableArray * tempArray = [doctorTemp objectForKey:dictTempKey];
for(UIView * subview in tempArray)
{
for(UIView * subview2 in [subview subviews])
{
if([subview2 isKindOfClass:[UILabel class]])
{
if([((UILabel*)subview2).text isEqualToString:except])
{
counterRow = i;
tempile = i;
yAxis=5.0+(i*40.0);
}
}
}
}
}
for(int j=tempile;j<=tempo;j++)
{
NSString *dictTempKey2 = [NSString stringWithFormat:#"row%d",j];
NSMutableArray * tempArray = [doctorAddition objectForKey:dictTempKey2];
for(UIView * subview in tempArray)
{
[subview removeFromSuperview];
}
[doctorAddition removeObjectForKey:dictTempKey2];
}
for(int k=tempile;k<=tempo;k++)
{
NSString *dictTempKey3 = [NSString stringWithFormat:#"row%d",k];
NSMutableArray * tempArray2 = [doctorTemp objectForKey:dictTempKey3];
for(UIView * subview in tempArray2)
{
for(UIView * subview2 in [subview subviews])
{
if([subview2 isKindOfClass:[UILabel class]])
{
if(![((UILabel*)subview2).text isEqualToString:except])
{
[self createDoctorBox:((UILabel*)subview2).text];
}
}
}
}
}
}
CreateDoctorBox method;
-(UIView*)createDoctorBox : (NSString*)name {
[addedList addObject:name];
NSString *myString = name;
CGSize stringSize = [myString sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:13]];
UIView *doktorKutu = [[UIView alloc]init];
[doctorList addSubview:doktorKutu];
UIView *doktorKutuBas = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 18, 36)];
[doktorKutuBas setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"doktor_isim_kutu_bas"]]];
[doktorKutu addSubview:doktorKutuBas];
UILabel * doktorKutuGovde = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, stringSize.width+3, 36)];
[doktorKutuGovde setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[doktorKutuGovde setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"doktor_isim_kutu_1px"]]];
[doktorKutuGovde setUserInteractionEnabled:YES];
[doktorKutuGovde setText:myString];
[doktorKutu addSubview:doktorKutuGovde];
UIView * doktorKutuKic = [[UIView alloc]initWithFrame:CGRectMake(stringSize.width+13, 0, 18, 36)];
[doktorKutuKic setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"doktor_isim_kutu_kic"]]];
[doktorKutu addSubview:doktorKutuKic];
UIImageView *cancelImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"cikar"]];
cancelImage.frame = CGRectMake(-5,9, 18, 18);
[doktorKutuKic addSubview:cancelImage];
UIButton *cancel = [[UIButton alloc]initWithFrame:CGRectMake(-5,0, 20, 36)];
[cancel setUserInteractionEnabled:YES];
[cancel addTarget:self action:#selector(removeNameFromTheList:) forControlEvents:UIControlEventTouchUpInside];
[doktorKutuKic addSubview:cancel];
UITapGestureRecognizer *singlePress =[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSinglePress:)];
[doktorKutuGovde addGestureRecognizer:singlePress];
[doktorKutu bringSubviewToFront:cancel];
[doctorCommented addObject:doktorKutu];
[doktorKutuKic bringSubviewToFront:cancel];
[doktorKutu bringSubviewToFront:cancel];
dictRowKey = [NSString stringWithFormat:#"row%d",counterRow];
NSMutableArray *row = [[NSMutableArray alloc]init];
doktorKutu.frame = CGRectMake(5, yAxis, stringSize.width+30, 36);
if([doctorAddition objectForKey:dictRowKey]!=nil)
{
row = (NSMutableArray*)[doctorAddition objectForKey:dictRowKey];
if(row.count>0)
{
int totalWidth = 5;
for(int i=0;i<row.count;i++)
{
totalWidth = totalWidth + ((UIView*)[row objectAtIndex:i]).frame.size.width+5;
}
if(totalWidth+stringSize.width<520)
{
doktorKutu.frame = CGRectMake(totalWidth, yAxis, stringSize.width+30, 36);
[row addObject:doktorKutu];
[doctorAddition removeObjectForKey:dictRowKey];
[doctorAddition setObject:row forKey:dictRowKey];
}
else
{
doktorKutu.frame = CGRectMake(5, yAxis+40, stringSize.width+30, 36);
yAxis= yAxis + 40.0;
counterRow++;
dictRowKey = [NSString stringWithFormat:#"row%d",counterRow];
row = [[NSMutableArray alloc]init];
[row addObject:doktorKutu];
[doctorAddition setObject:row forKey:dictRowKey];
}
}
}
else
{
[row addObject:doktorKutu];
[doctorAddition setObject:row forKey:dictRowKey];
}
[doctorList setContentSize:CGSizeMake(10, 36+(counterRow*41))];
return doktorKutu;
}
its because when your function " createDoctorBox " call, there may be you have changed a scrollview properties. Please check there.
in your code [doctorList setContentSize:CGSizeMake(10, 36+(counterRow*41))]; its reset the contentOffset of the UIScrollView. if you want to stay on position of delete button, then below that line, set the delete button point as contentOffset of UIScrollView.
Example code:
[doctorList setContentSize:CGSizeMake(10, 36+(counterRow*41))];
doctorList.contentOffset:CGMakePoint(0,deleteBtn.frame.origin.y);
if its help then please vote for me.

Fetching Selected Image From An Array to be displayed in another view

I'm trying to fetch the selected image from the array of images.And the selected image should be displayed in new view..
- (void)viewDidLoad{
[super viewDidLoad];
selectedTag = 0;
imagesName = [[NSArray alloc]initWithObjects:#"nature1.jpg",#"nature2.jpg",#"nature3.jpg",#"nature4.jpg",#"nature5.png",#"nature6.jpg",nil];
images = [[NSMutableArray alloc]init];
imageScroll.delegate = self;
imageScroll.scrollEnabled = YES;
int scrollWidth = 768;
imageScroll.contentSize = CGSizeMake(scrollWidth,605);
int xOffset = 0;
for(index=0; index < [imagesName count]; index++)
{
UIButton *img = [UIButton buttonWithType:UIButtonTypeCustom];
img.tag = index;
selectedTag = img.tag;
img.bounds = CGRectMake(10, 10, 50, 50);
img.frame = CGRectMake(50+xOffset, 120, 270, 350);
NSLog(#"image: %#",[imagesName objectAtIndex:index]);
[img setImage:[UIImage imageNamed:[imagesName objectAtIndex:index]] forState:UIControlStateNormal];
[images insertObject:img atIndex:index];
imageScroll.contentSize = CGSizeMake(scrollWidth+xOffset,510);
[imageScroll addSubview:[images objectAtIndex:index]];
xOffset += 370;
[img addTarget:self action:#selector(newView:) forControlEvents:UIControlEventTouchUpInside];
selectedTag = img.tag;
}
}
-(void)newView:(id)sender{
NSLog(#"%#",sender);
int f;
f = [sender tag];
CGRect rec = CGRectMake(0, 0, 250, 250);
[self setView:[[[UIView alloc] initWithFrame:rec] autorelease]];
[[self view] setBackgroundColor:[UIColor grayColor]];
UIImageView *im = [[UIImageView alloc] initWithImage:[imagesName objectAtIndex:f]];
im.bounds = CGRectMake(10, 10, 50, 50);
im.frame = CGRectMake(250, 120, 270, 350);
[[self view] addSubview:im];
}
Can anybody help me to fetch the selected image to another view?? Thanks In Advance..
You are setting string instead of image. Use this piece of code
UIImage *img = [UIImage imageNamed:[imagesName objectAtIndex:f]];
UIImageView *im = [[UIImageView alloc] initWithImage:img];

Resources