UIScrollview overlapping mainview content - uiview

Currently, I have this feature in my app where I implemented a UIScrollview that is pretty thin in height but long in width...
As you can see, the UIScrollView is OVERLAPPING the backgroundview... Not that white background is a UIView to which I added the UIScrollView as a SUBVIEW.
Question is, how is the UIScrollView overlapping the black background content when it's just added to the subview?
Here is my init method for the UIScrollView..
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, 280, 70);
self.contentSize = CGSizeMake(480, 70);
self.showsHorizontalScrollIndicator = NO;
self.bounces = NO;
}
And here is how I added the UIScrollView to the UIView (whitebackground view) whose name is ForecastView:
_hourlyForecast = [[hourlyForecastScrollView alloc] init:_city state:_state icons:_icons times:_times temps:_temps];
_hourlyForecast.backgroundColor = [UIColor blueColor];
_hourlyForecast.frame = CGRectMake(20, 20, self.ForecastView.bounds.size.width, 70);
[_ForecastView addSubview:_hourlyForecast];

By default, a view will not clip it's subviews. To enable clipping, set the UIView clipsToBounds property, or set it in Interface Builder
(credit to Mugunth for the image)

Related

Change width and add a circular scroller to a UIScrollView

I am trying to increase the width of the scrollbar and add a circular scroller. Shall I use an image for the circle? I do not see any property or method of UIScrollView to change the width of the scroller
I tried the following:
UIScrollView *myScroll = [[UIScrollView alloc] init];
myScroll.frame = self.view.bounds; //scroll view occupies full parent view!
//specify CGRect bounds in place of self.view.bounds to make it as a portion of parent view!
myScroll.contentSize = CGSizeMake(400, 800); //scroll view size
myScroll.backgroundColor = [UIColor grayColor];
myScroll.showsVerticalScrollIndicator = YES; // to hide scroll indicators!
myScroll.showsHorizontalScrollIndicator = YES; //by default, it shows!
myScroll.scrollEnabled = YES; //say "NO" to disable scroll
[myScroll setBackgroundColor:[UIColor redColor]];
myScroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[self.view addSubview:myScroll];
This custom ScrollView Solved all my problems https://github.com/BasheerSience/BRScrollBar

How to add vertical space between UINavigationBar and UISegmentedControl?

I'm working with my UI completely programmatically here. When I add a UISegmentedControl to my UITableViewController's header view, it just fixes itself up there with no vertical space between the UINavigationBar. How can I add some padding between the UISegmentedControl and the UINavigationBar ?
Instantiate a UIView object and add your UISegmentedControl as a subview. Then set the UIView as your table's headerView. You'll be able to add padding by adjusting the frame of the UIView you created.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150 /* <-- adjust this value for more or less padding */)];
UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithItems:#[#"One", #"Two", #"Three"]];
segControl.frame = CGRectMake(0, 90, 200, 29);
//calculate the middle of the header view
CGFloat middleOfView = headerView.bounds.size.width / 2;
CGFloat middleOfSegControl = segControl.bounds.size.width / 2;
CGFloat middle = middleOfView - middleOfSegControl;
//position the seg control in the middle
CGRect frame = segControl.frame;
frame.origin.x = middle;
segControl.frame = frame;
[headerView addSubview:segControl];
self.theTableView.tableHeaderView = headerView;
}
Of course you can mess with the frames some more to get things positioned like you want them.

Why a UILabel at Y position of 0 does not sit at top of screen

Context
This is a UIViewController which is within a UINavigationController stack
Within this UIViewController I'm adding a UILabel programmatically at (x,y) coordinates of (0,0)
I've experimented adding UILabel to self.view (this is within a UIViewController) or adding UILabel to a UIView, this UIView is self.containerView
self.containerView is created and added to the view through this code:
- (void)addContainerView
{
// Create a UIView with same frame as the screen bounds
CGRect containerViewFrame = [[UIScreen mainScreen] applicationFrame];
self.containerView = [[UIView alloc] initWithFrame:containerViewFrame];
// Give the UIView a red background
self.containerView.backgroundColor = [UIColor redColor];
// Add the view
[self.view addSubview:self.containerView];
}
The UILabel is added through this code:
- (void)addTestLabel
{
self.navigationController.navigationBarHidden = YES;
CGRect frame = CGRectMake(0, 0, 100, 100);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = #"this is a test";
[self.view addSubview:label]; // OR [self.containerView addSubview:label]
}
When UILabel is added to self.view
When UILabel is added to self.containerView
Questions
Why doesn't the UILabel sit right at the top of the screen, even behind the status bar?
Why is there a difference between the yPos, dependent on whether it is added to self.view or self.containerView
Change the background color of the label and I think you'll see what's going on. The height of the label is 100 pixels and it's vertically centering it within that space. Change the height to 20 or 30 and try it again.

UIScrollView at the bottom of a UIScrollView

I am looking for a way to implement the following:
Have one 'master' scrollView that contains both a full-screen UIView on top and a full-screen UIScrollView below this
When the user scrolls past the top UIView, the bottom scrollView is visible and becomes the responder for scroll events
When the user attempts to scroll up from the bottom UIScrollView, the touches are redirected so they control the 'master' scrollView and bring the UIView into view again.
To give an idea of how this is set out, here is my current implementation:
// Initialise components:
mainScreen = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = mainScreen.size.height-20;
// Scroll View Controller
_scrollControl = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, screenHeight)];
_scrollControl.contentSize = CGSizeMake(320, 2*screenHeight); // Twice as big as the screen size for both views to fit
_scrollControl.backgroundColor = [UIColor clearColor];
_scrollControl.delegate = self;
// Top View
_topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, screenHeight)];
_topView.backgroundColor = [UIColor redColor];
[_scrollControl addSubview:_topView];
// Bottom View
_bottomView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, screenHeight, 320, screenHeight)];
_bottomView.backgroundColor = [UIColor yellowColor];
_bottomView.contentSize = CGSizeMake(320, 2*screenHeight);
_bottomView.delegate = self;
UILabel *imageLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 700)];
imageLabel.backgroundColor = [UIColor greenColor];
[_bottomView addSubview:imageLabel];
[_scrollControl addSubview:_bottomView];
// Add to main view
[self.view addSubview:_scrollControl];
I have tried to achieve the desired effect using delegate methods, however I can't seem to stop the 'master' scrollView from scrolling before it switches to the bottom scrollView.
Apple provides this functionality for free, so the good news is you don't need to actually directly code anything here (unless you want something a little funky). The effect is achieved through paging
scrollView.pagingEnabled = YES;
Before this will take any real effect, you will need embed your subviews (both the UIView and the UIScrollView) in the master scroll view. Each of them should be the same size, or size of a single page. So let's say the master scroll view is 100, 100 points, we could set it up like so:
CGRect pageRect = CGRectMake(0, 0, 100, 100);
NSInteger pageCount = 2;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:pageRect];
scrollView.pagingEnabled = YES;
UIView *page1View = [[UIView alloc] initWithFrame:pageRect];
page1View.backgroundColor = [UIColor redColor];
[scrollView addSubview:page1View];
pageRect.origin.x += pageRect.size.width;
UIView *page2View = [[UIView alloc] initWithFrame:pageRect];
page2View.backgroundColor = [UIColor blueColor];
[scrollView addSubview:page2View];
scrollView.contentSize = CGSizeMake(pageRect.size.width * pageCount,
pageRect.size.height);
That should give you the basics. As you can see we position the subviews one after another horizontally in this example. We set the content size to cover both subviews and enable paging. UIScrollView should take care of the rest.
A good place to look is at the WWDC session views, specifically from WWDC 2010 at: Session 104 - Designing Apps with Scroll Views. This has a lot of information on how to setup scroll views and really get the most out of them.
Hope this helps!
so if you want to detect it using swift, use these:
override func scrollViewDidScroll(scrollView: UIScrollView) {
if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
//reach bottom
}
if (scrollView.contentOffset.y < 0){
//reach top
}
if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y < (scrollView.contentSize.height - scrollView.frame.size.height)){
//not top and not bottom
}
}

Adding UIScrollView to a UIViewController

I have a UIViewController, and I want to add a UIScrollView to it (enable scroll support), is it possible?
I know it is possible, if you have a UIScrollView to add a UIViewController to it, but I'm interested also if reverse was true, if I cann add a UIScrollView to an existing UIViewController, such that I get scrolling feature.
Edit
I think I have found an answer: Adding a UIViewController to UIScrollView
An UIViewController has a view property. So, you can add a UIScrollView to its view. In other words, you can add the scroll view to the view hierarchy.
This is can achieved by code or through XIB. In addition, you can register the view controller as the delegate for your scroll view. In this way, you can implement methods for performing different functionalities. See UIScrollViewDelegate protocol.
// create the scroll view, for example in viewDidLoad method
// and add it as a subview for the controller view
[self.view addSubview:yourScrollView];
You could also override loadView method for UIViewController class and set the scroll view as the main view for the controller you are considering.
Edit
I created a little sample for you. Here, you have a scroll view as a child of the view of a UIViewController. The scroll view has two views as children: view1 (blue color) and view2 (green color).
Here, I suppose you can scroll in only one direction: horizontally or vertically. In the following, if you scroll horizontally, you can see that the scroll view works as expected.
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
scrollView.backgroundColor = [UIColor redColor];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = YES;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, self.view.bounds.size.height);
[self.view addSubview:scrollView];
float width = 50;
float height = 50;
float xPos = 10;
float yPos = 10;
UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(xPos, yPos, width, height)];
view1.backgroundColor = [UIColor blueColor];
[scrollView addSubview:view1];
UIView* view2 = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width + xPos, yPos, width, height)];
view2.backgroundColor = [UIColor greenColor];
[scrollView addSubview:view2];
}
If you need to scroll only vertically you can change as follows:
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
Obviously, you need to rearrange the position of view1 and view2.
P.S. Here I'm using ARC. If you don't use ARC, you need to explicitly release alloc-init objects.

Resources