Adding subviews to an array of UIViews? - ios

I have a scroll view on which I have a main UIView. This main UIView has an array of UIViews in it.
I want to add a test view over this child view
for (NSUInteger i = 0; i<self.maxPage+1; i++) {
CanvasBaseView *view =[[CanvasBaseView alloc]initWithFrame:CGRectMake(0, (self.canvas.height.floatValue+NAPKIN_PADDING)*i, self.canvas.width.floatValue,self.canvas.height.floatValue)];
[view addSubview:[self setDrawView:view]];
}
-(UIView*)setDrawView:(UIView *)main{
UIView *vw =[[UIView alloc]initWithFrame:main.frame];
[vw setBackgroundColor:[UIColor blackColor]];
return vw;
}
But when I do this only the first view has my new black background View. I tried it with Array of 2,6,10 and all of this only the first view is being populated
Even I tried this
UIView *vw = [UIView new];
vw.frame = main.frame;
[vw setBackgroundColor:[UIColor blackColor]];
return vw;
Unfortunately same result.

If I understand correctly you want to add a black overlay to each child view in your loop. In that case you should use the bounds property of the parent view, as child views are relative it's parents view.
Change this line:
UIView *vw =[[UIView alloc]initWithFrame:main.frame];
To this:
UIView *vw =[[UIView alloc]initWithFrame:main.bounds];

Related

Subview of UILabel disappears when the text was changed to Unicode string

I am seeing a strange behavior of UILabel and its subview on iOS development.
I want to have a overlay view on the UILabel and want to change only the text of the label.
It works well when the text is digits or alphabets.
However when I changed the text to a Unicode string, the subview disappears.
To reproduce the problem, I created a simple project which just have a label and a button.
The label has a "A" as the initial text.
When the button was tapped, the following method will be called and change the text of UILabel cyclically.
- (void)pushButton:(id)sender
{
if ([[_label text] isEqualToString:#"A"]) {
[_label setText:#"B"];
// add a subview on the label
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[view setBackgroundColor:[UIColor blueColor]];
[_label addSubview:view];
} else if ([[_label text] isEqualToString:#"B"]) {
[_label setText:#"C"];
} else if ([[_label text] isEqualToString:#"C"]) {
[_label setText:#"D"];
} else if ([[_label text] isEqualToString:#"D"]) {
[_label setText:#"\u2605"];
// after this, the subview disappears
} else {
[_label setText:#"A"];
// after this, the subview appears again
}
}
When I tap the button at the first time, the text is changed to "B" and the subview appears.
Second time and third time, the text is changed to "C" and "D" and the subview is still there.
Howerver the fourth time, the text is changed to "★" and the subview disappears.
Fifth time, the text is changed to "A" and the subview appears again.
In this code, I don't remove the subview and the new subview is created in every cycle.
However in any time the text was changed to "★", all these subviews disappear.
How can I keep the subview being appeared on the UILabel?
UILabel is not intended to have subviews. The solution is to make your blue rectangle a subview of your label's superview. You can position it so that it appears in front of your UILabel.
Thus, where you have this code:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[view setBackgroundColor:[UIColor blueColor]];
[_label addSubview:view];
You would instead say this:
UIView *sup = [_label superview];
UIView *view = [UIView new];
[view setBackgroundColor:[UIColor blueColor]];
CGRect r = CGRectMake(0,0,20,20);
r = [sup convertRect:r fromView:_label];
view.frame = r;
[sup addSubview: view];

Page control on subview

I am creating a view with a subview for a tutorial screen.
I want the gray subview to be swipeable using the page control
I have implemented the example of http://www.appcoda.com/uipageviewcontroller-tutorial-intro/ but this works with whole ViewControllers and I want it to work with only the subview...
Does anyone have any idea how to implement the PageControl with the swipe gesture so that only the gray subview changes? Any good tutorial?
The grey area must be a UIScrollView with a content view containing each page of the tutorial. Set pagingEnabled to YES on your scroll view so that it will snap to each page when you scroll.
Then you need to attach an action to the page control using addTarget:action:forControlEvents: and pass UIControlEventValueChanged as the event. Then the action must be a method that tells the scroll view to move forward or back a page depending on whether the value of the page control increased or decreased. You can do this by changing the scroll view's content offset, or by telling it to scroll so that a particular rect is visible.
Finally, implement the delegate of the UIScrollView, and use the methods that tell when the scroll view stopped scrolling (you'll need a combination of the did end decelerating, did end dragging and possibly did end scrolling animation), and update the page control's value when the scroll view changes pages.
And that's all there is to it. If you need more details, read the documentation for UIScrollView, UIScrollViewDelegate and UIPageControl.
use UIView instead of ViewController
Here is a solution that works for me:
1) Create a scrollView and pageControl in your NIB or Storyboard
2) scrollView in ViewDidLoad
self.scrollView.backgroundColor = [UIColor clearColor];
self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack; //Scroll bar style
self.scrollView.showsHorizontalScrollIndicator = NO;
//dont forget to set delegate in .h file
[self.scrollView setDelegate:self];
self.scrollView.showsVerticalScrollIndicator = YES; //Close vertical scroll bar
self.scrollView.bounces = YES; //Cancel rebound effect
self.scrollView.pagingEnabled = YES; //Flat screen
self.scrollView.contentSize = CGSizeMake(640, 30);
3) create your views you want in the scrollView and add them in an Array
//For instance, you want 3 views
UIView *ViewOne = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
[ViewOne setBackgroundColor:[UIColor redColor]];
UIView *ViewTwo = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width+1, self.scrollView.frame.size.height)];
[ViewTwo setBackgroundColor:[UIColor greenColor]];
UIView *ViewThree = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width+2, self.scrollView.frame.size.height)];
[ViewThree setBackgroundColor:[UIColor blueColor]];
//add all views to array
NSMutableArray *viewsArray = [[NSMutableArray alloc] initWithObjects:ViewOne, ViewTwo, ViewThree, nil];
4) pageControl in ViewDidLoad
self.pageControl.numberOfPages = viewsArray.count;
self.pageControl.currentPage = 0;
self.pageControl.backgroundColor = [UIColor clearColor];
[self.pageControl setTintColor:[UIColor whiteColor]];
5) Add it all up
for(int i = 0; i < viewsArray.count; i++)
{
CGRect frame;
frame.origin.x = (self.scrollView.frame.size.width *i) + 10;
frame.origin.y = 0;
frame.size = CGSizeMake(self.scrollView.frame.size.width - 20, self.scrollView.frame.size.height);
NSLog(#"array: %#", [viewsArray objectAtIndex:i]);
UIView *view = [[UIView alloc] initWithFrame:frame];
view = [viewsArray objectAtIndex:i];
[self.scrollView addSubview:view];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width*viewsArray.count, self.scrollView.frame.size.height);
}
6) Track the scrollView and update the pageControl (DONT FORGET THE SCROLLVIEW DELEGATE)
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth = scrollView.frame.size.width;
//int page = floor((scrollView.contentOffset.x - pageWidth*0.3) / pageWidth) + 1);
self.pageControl.currentPage = (int)scrollView.contentOffset.x / (int)pageWidth;
NSLog(#"CURRENT PAGE %d", self.pageControl.currentPage);
}
This should do the trick.
PS. Sorry for all the magic numbers.

Changing the colour of UIView that is created dynamically

I am creating a UIView and its subviews (such as UITextfield, UIButton etc.) dynamically( by reading from JSON )
I am giving tags to all UIView and try to call by tag.
For instance I am trying to change the background colour of a view inside another view.
parent view's tag is 1, subview's is 11.
My sample code is as follows;
UIView *temp = [self.view viewWithTag:1];
if([[temp viewWithTag:11] isKindOfClass:[UIView class]])
{
[((UIView*)[temp viewWithTag:11]) setBackgroundColor:[UIColor blackColor]];
}
But there is no change, no effect.
Could you please help me on solving this.
#define PARENT_VIEW_TAG 1
#define CHILD_VIEW_TAG 11
UIView *parentView = [self.view viewWithTag:PARENT_VIEW_TAG];
for (UIView *vw in [parentView subviews]) {
if(vw.tag == CHILD_VIEW_TAG)
{
[vw setBackgroundColor:[UIColor blackColor]];
}
}

UIScrollView Center the View in Long Width

I have a UIScrollView in my ViewController that has 1600Width and 600Height. i Put 3 images there so the swiping from left to right is nice until the each end. My problem is in the 2nd photo in the middle. i can;t make the scrolling stop on exactly on it.. how can i make the UIVScrollView centered?
I used this code to scroll from out of bounds
[self.myScrollView setScrollEnabled:YES];
[self.myScrollView setContentSize:CGSizeMake(1600, 600)];
Please help me through this. your help will be very much appreciated.
-(void)creatScrollViewWithimage
{
UIScrollView *mainScrollview=[[UIScrollView alloc]initWithFrame:self.view.frame];
mainScrollview.pagingEnabled=YES;
for (int i=0; i<3; i++) {
CGRect newFrame=mainScrollview.frame;
newFrame.origin.x=mainScrollview.frame.size.width*i;
UIView *view=[[UIView alloc]initWithFrame:newFrame];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.view.frame];
[imageView setImage:[UIImage imageNamed:#"imagename.png"]];
[view addSubview:imageView];
[mainScrollview addSubview:view];
}
[mainScrollview setContentSize:CGSizeMake(3*mainScrollview.frame.size.width, mainScrollview.frame.size.height)];
[self.view addSubview:mainScrollview];
}
this will create scroll view for entire screen and add three UIImageView with paging enabled.

UIView in UIScrollView does not appear

I'm trying to create a horizontally scrollable menu similar to that in this video.
For some reason the UIView doesn't appear after adding a bunch of UIButtons to it and adding it to the UIScrollView. Here's my code (it's called in -viewDidLoad of a subclass of UIViewController):
//set up scrollview
UIScrollView *designPicker = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 431, 320, 49)];
//set up a view to drop into the scroll view
UIView * buttonsView = [[UIView alloc] initWithFrame:CGRectMake(0, 431, 640, 49)];
//add buttons to scrollview
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
float runningX = designPicker.frame.origin.x;
for (i = 1; i <= 10; i++)
{
UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tempBtn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
CGRect rect = CGRectMake(runningX, designPicker.frame.origin.y, 30.0, 30.0);
tempBtn.frame = rect;
[buttonsView addSubview:tempBtn];
runningX = runningX + 35;
[tempBtn release];
}
[designPicker setContentSize:buttonsView.frame.size];
[designPicker addSubview:buttonsView];
[self.view addSubview:designPicker];
You should not add the buttons using the frame of the UIScrollView. The origin of the frame is in the superview's (superview of the UIScrollView) coordinates. You should make the buttons' frame relative to the UIView. So if you want the buttons to appear at the top of the view that you should start at (0,0).
Instead of adding scrollview as subview to your view, add view as subview of scrollview. As-
[self.scrollView addSubview:self.view];
// release scrollView as self.view retains it
self.view=self.scrollView;
[self.scrollView release];
And make sure your view should have large content size than content size of your scrollview.It worked for me.

Resources