I'm creating a scrollView programmatically, and all the views in it. However, when I add more then 1 view, the other views layers don't show up right in their respective frames.
For example:
-(void)viewDidLoad {
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 150*5);
self.scrollView.backgroundColor = [UIColor lightGrayColor];
self.scrollView.scrollEnabled = YES;
self.scrollView.showsVerticalScrollIndicator = NO;
[self.view addSubview:self.scrollView];
[self setUpLogInButtons];
}
Then I set the subViews to scrollView up here:
-(void)setUpLogInButtons {
CGFloat subviewWidth = self.scrollView.frame.size.width/2;
CGFloat subviewHeight = 150;
UIView *facebookView = [[UIView alloc] init];
facebookView.frame = CGRectMake(0, 0, subviewWidth, subviewHeight);
[facebookView.layer insertSublayer:[self gradientLayerForViewFrame:facebookView.frame withColors:[self colorArrayWithValues:0x4A5C81 v2:0x495B82 v3:0x495B82 v4:0x475B85 v5:0x465B87 v6:0x455A88 v7:0x445A8A v8:0x435A8B v9:0x425A8D v10:0x41598F v11:0x405990 v12:0x3F5992 v13:0x3E5993 v14:0x3D5895 v15:0x3C5896 v16:0x3B5898 v17:0x3A589A]] atIndex:0];
[facebookView addSubview:[self label:self.facebookLabel forView:facebookView withName:#"Facebook" loggedIn:NO]];
[self.scrollView addSubview:facebookView];
}
When it's one subView it works fantastic, when I add the other one it's not right:
-(void)setUpLogInButtons {
...
UIView *facebookView = [[UIView alloc] init];
...
[self.scrollView addSubview:facebookView];
UIView *twitterView = [[UIView alloc] init];
twitterView.frame = CGRectMake(subviewWidth, 0, subviewWidth, subviewHeight);
[twitterView.layer insertSublayer:[self gradientLayerForViewFrame:twitterView.frame withColors:[self colorArrayWithValues:0x2BA8D5 v2:0x28A8D6 v3:0x25A8D8 v4:0x22A8D9 v5:0x20A9DB v6:0x1DA9DC v7:0x1AA9DE v8:0x18AADF v9:0x15AAE1 v10:0x12AAE3 v11:0x10ABE4 v12:0x0DABE6 v13:0x0AABE7 v14:0x08ACE9 v15:0x05ACEA v16:0x02ACEC v17:0x00ADEE]] atIndex:0];
[twitterView addSubview:[self label:self.twitterLabel forView:twitterView withName:#"Twitter" loggedIn:NO]];
[self.scrollView addSubview:twitterView];
}
This one does not show up, the layers specifically are off. However, here is the kicker, the label shows in the right spot because the labels frame is based on it's parent view frame.
-(UILabel *)label:(UILabel *)viewLabel forView:(UIView *)accountView withName:(NSString *)name loggedIn:(BOOL)loggedinStatus {
UILabel *label = viewLabel;
label.frame = CGRectMake(0, accountView.frame.size.height - 50, accountView.frame.size.width, 50);
label.text = [self setAccountLabelForName:name loggedIn:loggedinStatus];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentLeft;
return label;
}
here is a picture that shows you that the label is based off the parents view frame, and it shows up correctly, but the gradient does not, and it's based off the same frame.
and my gradient method:
-(CAGradientLayer *)gradientLayerForViewFrame:(CGRect)viewFrame withColors:(NSArray *)colors {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = viewFrame;
gradient.colors = colors;
return gradient;
}
For some reason when inserting my gradient layer to the views layer I had to change it:
//INSTEAD OF THIS:
[... insertSublayer:[self gradientLayerForViewFrame:facebookView.frame...];
//THIS WORKED FLAWLESSLY:
[... insertSublayer:[self gradientLayerForViewFrame:facebookView.bounds...];
Related
I'm trying to size an MKMapView in the following way:
Elements should be able to appear behind the arc at the top, so it appears that the view is not square. I know this should be possible with CALayer, but perhaps someone has done it before?
This should be quite possible if you put your MKMapView within a UIView, then apply a mask to that.
Here's a very (!) basic example:
All I've done is put my map in a UIView called "mapContainer" and added a mask to it using this:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGRect rect = CGRectInset(self.view.frame, 70, 70);
UIView* newView = [[UIView alloc] initWithFrame:rect];
newView.layer.cornerRadius = 55.0;
newView.backgroundColor = [UIColor redColor];
self.mapContainer.layer.mask = newView.layer;
}
And you could add a border to it...
..with a few more lines...
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGRect rect = CGRectInset(self.view.frame, 70, 70);
UIView* newView = [[UIView alloc] initWithFrame:rect];
newView.layer.cornerRadius = 55.0;
newView.backgroundColor = [UIColor redColor];
UIView* borderView = [[UIView alloc] initWithFrame:rect];
borderView.backgroundColor = [UIColor clearColor];
borderView.layer.cornerRadius = 55.0;
borderView.layer.borderWidth = 3.0;
borderView.layer.borderColor = [[UIColor redColor] CGColor];
[self.mapContainer addSubview:borderView];
[self.mapContainer bringSubviewToFront:borderView];
self.mapContainer.layer.mask = newView.layer;
}
I hope this points you in the right direction.
Unlike Apple Maps.
;-)
I'm trying to implement a check if a view is bounds of another view to do something. This my code:
CGRect movingView = [self.viewController.view convertRect:recognizer.view.frame toView:self.viewController.view];
for (NSUInteger i = 0; i < arrayOfViewsToCheck.count; i++)
{
UIView *view = resultArray [0];
CGRect boxframe = [self.viewController.view convertRect:view.frame toView:self.viewController.view];
if (CGRectIntersectsRect(movingView.frame, view.frame) )
{
isInBounds = YES;
}
}
But when I execute my code and for example I have this two views:
(CGRect) $6 = origin=(x=569, y=513) size=(width=76, height=100)
(CGRect) $7 = origin=(x=358, y=520) size=(width=116, height=100)
are in bounds of each other but of each I don't know what I'm doing wrong because it seems like they are not on my code.
I'll really appreciate if can please let me know what I'm doing wrong trying to detect if the views are in bounds of each other
Your use of CGRectIntersectsRect is correct, but the rectangles in your example do not actually intersect. Here's what they look like:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect rect1 = CGRectMake(569,513,76,100);
CGRect rect2 = CGRectMake(368,520,116,100);
UIView *view1 = [[UIView alloc] initWithFrame:rect1];
UIView *view2 = [[UIView alloc] initWithFrame:rect2];
view1.backgroundColor = [UIColor redColor];
view2.backgroundColor = [UIColor blueColor];
[self.view addSubview:view1];
[self.view addSubview:view2];
}
And with a slightly modified version:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect rect1 = CGRectMake(469,513,76,100); /* Note 469 instead of 569 */
CGRect rect2 = CGRectMake(368,520,116,100);
UIView *view1 = [[UIView alloc] initWithFrame:rect1];
UIView *view2 = [[UIView alloc] initWithFrame:rect2];
view1.backgroundColor = [UIColor redColor];
view2.backgroundColor = [UIColor blueColor];
[self.view addSubview:view1];
[self.view addSubview:view2];
if (CGRectIntersectsRect(rect1, rect2) ) {
NSLog(#"Yay we intersect!");
}
}
I happened to find that setContentOffset to a UIScrollView will cause the scrollView append a new view to its subviews. here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((320 - 261) / 2, 50, 261, 67)];
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = NO;
scrollView.backgroundColor = [UIColor darkGrayColor];
for (int i = 0; i < 5; i ++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * 87, 1, 87, 65)];
view.tag = i;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 30)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = [NSString stringWithFormat:#"%d", i];
[view addSubview:label];
view.backgroundColor = [UIColor greenColor];
[scrollView addSubview:view];
}
scrollView.contentSize = CGSizeMake(87 * 5, 67);
NSLog(#"before scroll: count of subview is %d", scrollView.subviews.count);
CGPoint offset = CGPointMake(87, 0);
[scrollView setContentOffset:offset animated:YES];
NSLog(#"after scroll: count of subview is %d", scrollView.subviews.count);
[self.view addSubview:scrollView];
}
Before calling setContentOffset:offset, the number of subviews of the scrollView is 5. This is what I expect. After that, the number turns to be 6.
Is it working as designed? How to avoid the new subview appended?
The extra subview is the scroll indicator. If you check again in a later method, it will have gone away. Don't worry.
You can confirm this by hiding the scroll indicators (showsVerticalScrollIndicator and showsHorizontalScrollIndicator properties).
Don't try to assume things about the view hierarchy of UIKit classes. UIKit can, and does, add its own views to several things - see tableviews and their cells, a navigation controller's view and so on.
I have a custom cell that I am controlling the color of when it is selected, ie the example code below:
UIView *selectedBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 600, 600)];
[selectedBackground setBackgroundColor:[UIColor selectedCellColor]];
self.selectedBackgroundView = selectedBackground;
This works, however I would only like to have part of the cell change colors when selected. My custom cell is broken down into many different subviews, and I have it sectioned out where I would be able to define the specific view that I would like to change colors for.
How can I control the selectedBackgroundView, or use a different method, to have the background color change encompass a single subview in my cell?
Ya you are in the rite way,by subclassing the UITableView cell
hear is the sample code that you may find the answer for your question :)
//in subclassed cell class
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.frame = CGRectMake(0, 0, 334, 250);
UILabel *aLabel1= [[UILabel alloc]init];
UILabel *aLabel2 = [[UILabel alloc]init];
self.label1 = aLabel1;
self.label2 = aLabel2;
self.label1.text = #"Happy";
self.label2.text = #"coding";
UIImageView *bg1 = [[UIImageView alloc]init];
bg1.tag = 100;
UIImageView *bg2 = [[UIImageView alloc]init];
bg2.tag = 200;
[self addSubview:bg1]; // you must add your background views first
[self addSubview:bg2];
[self addSubview:label1];//then other views
[self addSubview:label2];
[aLabel1 release];
[aLabel2 release];
[bg1 release];
[bg2 release];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// Configure the view for the selected state
// hear only you can manage your background views, simply i am adding 2 imageviews by setting different colors
[super setSelected:selected animated:animated];
self.backgroundColor = [UIColor greenColor];
if(selected)
{
self.label1.backgroundColor = [UIColor redColor];
self.label2.backgroundColor = [UIColor brownColor];
UIImageView *bg1 = (UIImageView *)[self viewWithTag:100];
bg1.frame = CGRectMake(0, 0,334/2, 250);
bg1.backgroundColor = [UIColor yellowColor];
}
else
{
self.label1.backgroundColor = [UIColor brownColor];
self.label2.backgroundColor = [UIColor redColor];
UIImageView *bg2 =(UIImageView *) [self viewWithTag:200];
bg2.frame = CGRectMake(35, 0, 334/2, 250);
bg2.backgroundColor = [UIColor lightGrayColor];
}
}
-(void)layoutSubviews
{
//i am setting the frame for each views that i hav added
[super layoutSubviews];
self.label1.frame = CGRectMake(10, 10, 60, 35);
self.label2.frame = CGRectMake(65, 10, 60, 35);
}
hope helps u :)
note: i am using "without ARC"
I have a UITextView inside a UIScrollView that needs to automatically scroll once the user starts editing it. This is because the keyboard will cover the textview.
Here is the code -
In viewDidLoad:
feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, segmentedControl.frame.origin.x + self.segmentedControl.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
feedBackformView.backgroundColor = [UIColor whiteColor];
feedBackformView.scrollEnabled = YES;
feedBackformView.delegate = self;
feedBackformView.userInteractionEnabled = YES;
feedBackformView.showsVerticalScrollIndicator = YES;
feedBackformView.contentSize = CGSizeMake(320, 700);
commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, emailField.frame.origin.y + 40, 250, 150)];
commentsView.delegate = self;
commentsView.layer.borderWidth = 2.0f;
commentsView.layer.cornerRadius = 5;
and here's the delegate method implementation -
-(void)textViewDidBeginEditing:(UITextView *)textView{
CGPoint point = textView.frame.origin;
[scrollView setContentOffset:point animated:YES];
}
However, nothing happens.
your are doing fine but use feedBackformView instead of scrollView,while setting content offset in textViewDidBeginEditing:method, just have a look on below code
feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 320, 200)];
feedBackformView.backgroundColor = [UIColor whiteColor];
feedBackformView.scrollEnabled = YES;
feedBackformView.delegate = self;
feedBackformView.userInteractionEnabled = YES;
feedBackformView.showsVerticalScrollIndicator = YES;
feedBackformView.contentSize = CGSizeMake(320, 700);
commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, 40, 250, 150)];
commentsView.delegate = self;
commentsView.layer.borderWidth = 2.0f;
commentsView.layer.cornerRadius = 5;
[feedBackformView addSubview:commentsView];
[self.view addSubview:feedBackformView];
in textview DelegateMethod,
-(void)textViewDidBeginEditing:(UITextView *)textView{
CGPoint point = textView.frame.origin;
[feedBackformView setContentOffset:point animated:YES];
}
hope it will hepls you...
You can use scrollView scrollRectToVisible:animated or the setContentOffset:animated methods described here:
UIScrollView Class Reference
Keep in mind that UITextView comes with its own scroll view. so you may not need to nest it inside another scrollview. But you may need to if your app is that interesting.
Is your point of scrolling already at a visible point?
-(void)textViewDidBeginEditing:(UITextView *)textView{
// try this instead
CGPoint point = CGPointMake(textView.frame.origin.x,
textView.frame.origin.y + textView.frame.size.height);
[scrollView setContentOffset:point animated:YES];
// What does this produce?
NSLog(#"%# %#", NSStringFromCGPoint(scrollView.contentOffset),
NSStringFromCGRect(textView.frame));
}