ScrollView won`t scroll after object is placed - scrollview

I guess this will be embracing but i start to get really frustrated:
I`m writing code for the first time so please help.
Working with X-Code, objective-C.
I would like to be able to scroll on my App-Pages, so i added a UIScrollView:
#interface HeggViewController : UIViewController
{
IBOutlet UIScrollView *scroller;
}
#implementation HeggViewController
- (void)viewDidLoad
{
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 1000)];
[super viewDidLoad];
It works fine until i add something. Button, Picture, label......
Can i just add buttons to the ScrollView or do i need to place something else first
What have i done wrong?
Thanks for the help..

You didn't show your code to add the objects, but if you do the following, it must work fine:
// Create your image
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(79, 20, 100, 100)];
// Add the image to the scrollview
[scroller addSubview:image];

Related

Xcode UIView and subviews

first of all, I am beginner with xcode and programming in Objective-c. I made my app with navigation bar and I have UIView with class Lesson1 and I added new subview Level1 but I dont wanna add new class. Is it any solution how add label to subview Level1 from class Lesson1.m?
Thank you
It is very easy to do so. Let's admit this is your Lesson1 view code. In the viewDidLoad method you can add whatever you want.
- (void)viewDidLoad
{
UIView *level1 = [[UIView alloc] initWithFrame:CGRectMake(x,y,width,height)];
[self.view addSubview:level1]
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(label_x,label_y,label_width,label_height)];
label1.text = #"labeltext";
[level1 addSubview:label1]
}
Yes, you can.
Programmatically:
// viewDidLoad method from Lesson1 class
- (void)viewDidLoad
{
self.level1 = // Your UIView
[self.view addSubview:self.level1]
[self.level1 addSubview:yourLabel]
}
Using XIB file for Lesson1:
Drag and drop an UILabel into your Lesson1 and link the UILabel with an IBoutlet on your Lesson1 class.
Use below link. It might be helpful for you!!
http://www.techotopia.com/index.php/An_iOS_7_Core_Data_Tutorial

EXC_BAD_ACCESS when dragging UIScrollView

In a controller I'm creating a UIScrollView. I'm trying to set this viewcontroller as the UISCrollview delegate and to implement the delegate's method in order to add (later) a UIPageControl.
I've read a bit, and found this link, this other link and other here on SO, and some useful tutorial all around the web, but I don't understand what I'm doing wrong. Everytime a scroll the UIScrollView, the app crashes with an EXC_BAD_ACCESS error.
Here's my .h file
#import <UIKit/UIKit.h>
#interface StatsViewController : UIViewController <UIScrollViewDelegate> {
UIScrollView *scrollView;
UIPageControl *pageControl;
}
#end
Then in my .m file, I'm creating the scrollview and trying to define the delegate method like this:
- (void)viewDidLoad {
[super viewDidLoad];
NSInteger boxWidth = self.view.frame.size.width;
NSInteger boxHeight = 412;
scrollView = [ [UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, boxHeight)];
scrollView.pagingEnabled = TRUE;
scrollView.delegate = self;
NSInteger numberOfViews = 2;
StatBreatheCounter *breatheCounter = [ [StatBreatheCounter alloc] init];
breatheCounter.view.frame = CGRectMake(0, 0, boxWidth, boxHeight);
[scrollView addSubview:breatheCounter.view];
BreatheLocationViewController *breatheLocation = [ [BreatheLocationViewController alloc] init];
breatheLocation.view.frame = CGRectMake(320, 0, boxWidth, boxHeight);
[scrollView addSubview:breatheLocation.view];
scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, boxHeight);
[self.view addSubview:scrollView];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
NSLog(#"RUNNING");
}
...but every time I slide on the scroll view, the app is crashing.
Now, I'm quite a n00b on Ojective-C, but I feel I'm missing something. Browsing around everything points on the fact that the delegate could be deallocated early, and when the user trigger the action, no one is handling the method (sorry for the explanation :)).
...but if the delegate it's the viewcontroller itself, how could it be deallocated?
As you can see, I'm quite confused :(
Any help would be really appreciated
--
EDIT:
I'm going to include here the solution founded thanks with your comments and answer.
When I posted my question I was so convinced that the error was coming from the way I was creating the UIScrollView and setting its delegate that I didn't realize that the problem was (as everything was suggesting, btw :)) I was allocating the StateViewController in its parent without declaring any "strong" reference to it (again, sorry for the explanation, I'm really a n00b in this).
Thanks a lot for your helping in pointing me on the right direction
It looks like you are losing reference to the delegate during scroll. I would look into any other release events around StatsViewController or other events that could cause it to be dereferenced.

UITableView Overlay when section index touched

I have a very custom table view that actually serves as a content view, but table view was the obvious choice. I have a section index that i use to scroll the TableView - but there are no sections (well, one is there obviously). For the purpose of the user's orientation, I'd like to fade a view over the table view that is semi-transparent and shows a text in there. It should look like the overlay with the letters when scrolling the new iPod nano's section index. I don't know where i should put the code - because my view has to disappear sometime again too, and I don't really wanna use notifications. I'd init the view inside the tableview: sectionForSectionIndexTitle method. Thanks in advance.
Create a property in your .h file
#property (nonatomic, retain) UILabel *overlayLabel;
And add the following code to your .m file
- (void)viewDidLoad {
[super viewDidLoad];
self.overlayLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f,
0.0f,
self.tableView.frame.size.width,
self.tableView.frame.size.height)] autorelease];
overlayLabel.backgroundColor = [UIColor clearColor];
overlayLabel.alpha = .5f;
overlayLabel.textAlignment = UITextAlignmentCenter;
overlayLabel.text = #"Some text";
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView addSubview:overlayLabel];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[overlayLabel removeFromSuperview];
}

Add a UITableViewControler to a UIView

I am writing an appcelerator module, which means I am handed a subclassed UIView to work with and create my visual controls in Objective C.
I am trying to add a tableview with a searchbar, but most samples online use rootViewController and UITableViewControler.
so...in order to add a tableview to the current view, do I need to create a tableview and a UITableViewController and add them somehow as subviews to the current view ?
I tried adding a MainViewController.h & MainViewController.m which is defined as
#interface MainViewController : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>
and then in my view
#import "MainViewController.h"
- (void)viewDidLoad
{
mainView = [[MainViewController alloc] init];
[self addSubview:mainView.view];
}
-(void)frameSizeChanged:(CGRect)frame bounds:(CGRect)bounds
{
if(CGRectIsEmpty(self.frame))
{
self.frame = bounds;
[self addSubview:mainView.view];
}
}
but it did not work, I just got an empty view. any ideas ? a sample code would be greatly appreciated
thanks
You could try something like this:
- (void)viewDidLoad {
self.view.backgroundColor=[UIColor whiteColor];
UITableView *TableListView=[[UITableView alloc] initWithFrame:CGRectMake(-5,-1,331,425) style:1];
TableListView.editing=NO;
TableListView.delegate=self;
TableListView.dataSource=self;
TableListView.separatorColor=[UIColor colorWithRed:0.000000 green:0.591928 blue:1.000000 alpha:1.000000];
TableListView.separatorStyle=1;
TableListView.rowHeight=40;
TableListView.tag=0;
TableListView.backgroundColor=[UIColor groupTableViewBackgroundColor];
TableListView.clipsToBounds=YES;
[self.view addSubview:TableListView];
[TableListView release];
}
Hope this helps get you started...

UIScrollView ScrollRectToVisible - not working with animate = yes

I have a UIScrollView which contains a button.
When the button is pressed, I would like to scroll to the bottom of the view using scrollRectToVisible.
eg:
CGRect r = CGRectMake(0, myUIScrollView.contentSize.height - 1, 1, 1);
[myUIScrollView scrollRectToVisible:r animated:YES];
If I set animated to NO, everything works as expected,
but if I set it to YES, I see really weird behaviour:
basically, nothing happens.
if I tap the button repeatedly, it may scroll a couple pixels,
or may scroll all the way.
but if I scroll the view manually with a finger before pressing the button,
it has a chance of scrolling to the bottom as expected, but it's not a sure thing.
I've printed _geScroll_Settings.contentSize, and it's as-expected.
I've also tried to delay the call to scrollRectToVisible by starting a timer, but the results are pretty much the same.
The scrollView is fairly vanilla.
I'm creating it in interface builder.
I am dynamically adding the scrollView's content at startup, and adjusting it's contentSize appropriately, but all that seems to be working fine.
Any thoughts?
My bet is that scrollRectToVisible is crapping out because the visible area is not valid (1x1), or the y offset is just outside the bounds, have you tried setting it with the size of the visible area of the scrollView instead?
CGRect rectBottom = CGRectZero;
rectBottom.size = myUIScrollView.frame.size;
rectBottom.origin.y = myUIScrollView.contentSize.height - rectBottom.size.height;
rectBottom.origin.x = 0;
[myUIScrollView scrollRectToVisible:rectBottom animated:YES];
Sorry I can't help you out more, but I'm not on my Mac right now, so I can't run a test. The code above would create a CGRect of the exact size of what fits inside the scrollView visible portion, and the offset would be the last visible portion in it.
I encountered a similar problem, including the "If I set animated to NO, everything works as expected" part.
It turned out that on iOS 6 the UITextView auto scrolls its nearest parent UIScrollView to make the cursor visible when it becomes first responder. On iOS 7 there is no such behavior. The UIScrollView seems to get confused by two calls to to scrollRectToVisible at about the same time.
On iOS 6 my explicit call to scrollRectToVisible is ignored most of the time. It will only scroll to make the first line of the UITextView visible (the auto scroll) and not the whole thing as it does on iOS 7.
To test it, make a new single view app in Xcode 5, set its deployment target to 6.0 and use the code below for the ViewController.m. Run it in the iOS 6.1 simulator, scroll to make the UITextView hidden and tap anywhere on the screen. You might have to retry it a few times, but in most cases it will only make the first line visible. If you re-enable the WORKAROUD define the UITextView gets embedded in its own UIScrollView and the call to scrollRectToVisible works as expected.
#import "ViewController.h"
//#define WORKAROUND
#interface ViewController ()
#property (nonatomic, strong) UIScrollView *scrollView;
#property (nonatomic, strong) UITextView *textView;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewTap)]];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
self.scrollView.contentSize = CGSizeMake(320, 400);
self.scrollView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.scrollView];
#ifdef WORKAROUND
UIScrollView* dummyScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 280, 280, 100)];
self.textView = [[UITextView alloc] initWithFrame:dummyScrollView.bounds];
[dummyScrollView addSubview:self.textView];
[self.scrollView addSubview:dummyScrollView];
#else
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 280, 280, 100)];
[self.scrollView addSubview:self.textView];
#endif
self.textView.backgroundColor = [UIColor grayColor];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewTap
{
if (self.textView.isFirstResponder) {
[self.textView resignFirstResponder];
}
else {
[self.textView becomeFirstResponder];
}
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
#ifdef WORKAROUND
[self.scrollView scrollRectToVisible:CGRectInset(self.textView.superview.frame, 0, -10) animated:YES];
#else
[self.scrollView scrollRectToVisible:CGRectInset(self.textView.frame, 0, -10) animated:YES];
#endif
}
#end