I am new in iphone development.. I have added the scrollview in my app and added 10 buttons graphically inside the scrollview. but when I run the scrollview is not scrolling
my code is as follow
- (void)viewDidLoad
{
[super viewDidLoad];
[scrollview setContentSize:CGSizeMake(1500,50)];
[scrollview setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:scrollview];
}
It looks like you are taking scrollview in xib. So, you need to remove [self.view addSubview:scrollview]; from your code and you can directly set properties of that scrollview in xib except setContentSize(that you need to set in .m file).
Or if you want to create scrollView manually, remove from xib and use this:
UIScrollView * content = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[content setContentSize:CGSizeMake(width, height)];
//Here you can add those buttons to content view
[self.view addSubView:content];
Related
In my application i have a view
in that i have many UIElements, buttons, etc..
I need to set
UserInteractionEnabled:NO for all the ui elements in that view except one button.
I tried with
[self.view setUserInteractionEnabled:NO];
The Require button also the subview that self.view that button also apply same behavior.
I can able to apply individually but it is not a good way.
How should i set UserInteractionEnabled:NO for all the other ui elements except one button
for (UIView *view in [self.view subviews])
{
if (view.tag==101)
[ view setUserInteractionEnabled:YES];
else
[ view setUserInteractionEnabled:NO];
}
Check this out:
for (UIView *view in self.view.subviews) {
if (!([view class]==[UIButton class]) )
{
view.userInteractionEnabled = NO;
}
}
You can just add transparent subview in the front of your view and place button on this transparent view:
UIView* maskView = [[UIView alloc] initWithFrame:self.view.bounds];
maskView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:maskView];
[maskView addSubview:buttonView];
And be sure that this transparent view is the last added subview, or just push it to the front of view in viewWillAppear:
[self.view bringSubviewToFront:maskView];
I have added few UI components using Storyboard (UILabel, UIImageView). I am trying to put UIScrollView in this view, below all other elements:
Here is my code right at the end of -viewWillAppear method (code from my View Controller):
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view addSubview:scroll];
[self.view sendSubviewToBack:scroll];
The reason why I need a UIScrollView is large amount of text in detailsLabel - I'm attaching code I use for handling this label programatically:
detailsLabel.text = #"Some long text taken from databse";
[detailsLabel sizeToFit];
detailsLabel.numberOfLines = 0;
detailsLabel.lineBreakMode = UILineBreakModeWordWrap;
What I am doing wrong? How do I put UIScrollView below all other elements of my main view?
Try this:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view insertSubview:scroll atIndex:0];
...
[self.view insertSubview:detailsLabel atIndex:1];
Put your code in ViewDidLoad . Then add the labels after you have added the UIScrollView to the self.View in the ViewDidLoad itself. So by default the UIScrollView would be added on the self.View and then your label. Now you can either add the label on the scrollview or onto the self.view .
I've followed the instructions in this answer and it worked great: I've changed the Custom Class in Identity Inspector from UIView to UIScrollView and added this line to my -viewDidLoad in the View Controller file:
[(UIScrollView *)self.view setContentSize:CGSizeMake(320, 700)];
Option One:
Declare detailsLabel in .h:
IBOutlet UILabel * detailsLabel; // MAP IT WITH NIB or STORYBOARD
give it property and Synthesise in .m.
here in viewDidLoad declare:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view addSubview:scroll];
[self.view sendSubviewToBack:scroll];
[scroll addSubview:detailsLabel];
// IT SHOULD WORK.
Option two:
If you have large amount of text to display, then you can also use UITextView, it has inbuilt scrollview. And you can also set its text edit property.
I have a full screen UIScrollView to display my image. An UIActivityIndicatorView is added to the UIScrollView, it spinning well, but how could i make it always spinning in the middle of the screen while I am scrolling, zooming, rotating?
If you add the UIActivityIndicatorView directly to the scroll view it will scroll with the scroll view. But, if you add it to the parent of the scroll view it will remain where it was placed. So, the solution is to add it to the parent of the scroll view.
Notes:
I would recommend having a UIViewController in your window, and then adding these both to the UIViewController.
See the discussion here about adding views directly to your window:
View Controller being sent a message even though it has been deallocated
In ur .h file
UIView *primaryImage;
UIView *secondaryImage;
UIActivityIndicatorView *indicator;
In ur .m file
-(void)indicatorView
{
primaryImage = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
primaryImage.backgroundColor = [UIColor blackColor];
primaryImage.alpha =0.5;
//[self.view.superview insertSubview:primaryImage aboveSubview:self.view.superview];
//[theTableView addSubview:primaryImage];
[self.view addSubview:primaryImage];
secondaryImage = [[UIView alloc] initWithFrame:CGRectMake(127.50,215,65,50)];
secondaryImage.backgroundColor = [UIColor blackColor];
secondaryImage.alpha = 0.9;
secondaryImage.layer.cornerRadius = 12;
[primaryImage addSubview:secondaryImage];
indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(30, 25, 25, 25)];
indicator.center = CGPointMake(32, 25);
//[indicator hidesWhenStopped];
[indicator startAnimating];
[secondaryImage addSubview:indicator];
}
-(void)dismissCoverImageView {
[indicator stopAnimating];
[indicator removeFromSuperview];
[secondaryImage removeFromSuperview];
[primaryImage removeFromSuperview];
}
and after that you can call [self indicatorView];
and [self dismissCoverImageView];
Define the UIScrollViewDelegate of your UIScrollView. And in the delegate method –(void)scrollViewDidScroll:(UIScrollView *)scrollView change the frame of UIActivityIndicator object.
I have created XIB file in freeform size because I need many controls to add. I have done it but when I push my view controller, it doesn't have automatic scrollbar on iPhone display and my view gets cropped! How to get my full view displayed on iPhone?
I have set view frame on view load but that doesn't make any difference!
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setFrame:CGRectMake(0, 0, 320, 600)];
}
Following looks like cycling between view and scrollview worked for me! Not a good solution though!
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0,0,320,600);
[self.view setFrame:frame];
self.scrollview = [[UIScrollView alloc] initWithFrame:frame];
[self.scrollview setBackgroundColor:[UIColor blackColor]];
[self.scrollview addSubview:self.view];
self.scrollview.contentSize = frame.size;
self.view = self.scrollview;
}
Thanks.
You should take a look at UIScrollView. Within the UIScrollView add a UIView for your content. You then need to set the content size of the UIScrollView to enable scrolling.
Example:
CGRect frame = CGRectMake(0,0,320,600);
UIView *subView = [[UIView alloc] initWithFrame:frame];
// Add objects to view
[self.scrollView addSubview:subView];
self.scrollView.contentSize = frame.size;
Hope this helps....
You have to take different approach for this case.
You need to add a UIScrollView to your controller's view - make it take the whole screen and then inside the scroll view you can add as much content as you wish - this way your form will be scrollable and the user can go up and down to see all the controls inside the form
I have created a UIScrollView and am now trying to place a UIButton over the scroll view. However when I build and run the application the scroll view still works fine but I cannot see the UIButton.
I link the UIButton IBOutlet inside the interface builder.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView.backgroundColor = [UIColor blackColor];
scrollView.delegate = self;
scrollView.bounces = NO;
backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"auckland-300.jpg"]];
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"249a-134206f1d00-1342071f5d9.ImgPlayerAUCKLAND.png"]];
// Note here you should size the container view appropriately and layout backgroundImage and image accordingly.
containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,601,601)];
playButton = [[UIButton alloc] init]; //test button cannot see it.
[containerView addSubview:backgroundImage];
[containerView addSubview:image];
scrollView.contentSize = containerView.frame.size;
[scrollView addSubview:containerView];
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 31.0;
[scrollView setZoomScale:scrollView.minimumZoomScale];
self.view = scrollView;
}
Any help would be appreciated
Everything seems ok. But I think the reason you are not able to see the button playButton is because you are not adding it to the view itself.
Dont you need to do this ? [containerView addSubview:playButton];
To help you out, here's what I do for debugging -
UIView implements a useful description method. In addition, it
implements a recursiveDescription method that you can call to get a
summary of an entire view hierarchy.
NSLog(#"%#", [controller.view recursiveDescription]);
The button is probably in the nib that you linked the controllers view to, right? By assigning the scrollview to the view, you remove the view from
The nib from the controller, that's why you can't see te button or press it.
You can either place the button in the scrollview or you add both scrollView and thenthe playButton as subviews of self.view.
Maybe you want to rethink your design tho.
Placing a button over a scrollview doesn't really seem like good practice to me.