I researched a lot but I cannot find a proper solutions. I want to add a radial background to a UIViewController which fits to a iPhone 4 and iPhone 5 screen (view See attached screenshot). The background should be done programmatically because i have several Scrollview with a height more than view.bounds.
Does anybody know a solution?
You can follow two different ways to achieve this.
The simple way. If you have an image (you posted a screenshot) wrap it into an UIImageView.
Once done you can add it as a subview of your controller's view sending it to back like the following.
UIImageView* radialImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"radialBackground"]];
[radialImage setFrame:[[self view] bounds]];
[[self view] addSubview:radialImage];
[[self view] sendSubviewToBack:radialImage];
The not simple one. Create a custom UIView (named RadialBackgroundView for example) and override drawRect: method.
- (void)drawRect:(CGRect)rect
{
// Take a look to CGContextDrawRadialGradient to draw radial gradient
}
You can use it like a simple image.
RadialBackgroundView* radialView = [[RadialBackgroundView alloc] initWithFrame:[[self view] bounds]];
[[self view] addSubview:radialView];
[[self view] sendSubviewToBack:radialView];
Related
I am working programmatically an application for iOS based on a ViewController. I am trying to do so programmatically as I want to understand the underlying concepts.
I have created a subclass of UIImageView and initialized this using an image. In the initialization method I added also a second UIImageView as I would like to handle the two differently but be part of the same object. Ultimately I would like to be able to scale the object (and hence the 2 UIImages) according to the device screen resolution (e.g. if resolution is low then I will scale the two images by 50%). I want to do this because I would like to be able to implement a zoom in and zoom out feature as well as supporting multiple resolutions and screen layouts.
Additional information:
The two images have different size (500x500 pixels) and (350x350
pixels).
My questions are:
how do I position the second image exactly in the center of the first? (I used the center property of the main UIImage but I think I got it wrong.. I thought that the center was the exact center of the square but either I am using it incorrectly or there is something I am missing)
are there any negative side effects for using this approach (UIView subclass class containing an additional UIView?) (E.g. Is it going to create confusion when applying transformation algorithms? Does it reduce the randering speed? Or more simply is it a bad design pattern?)
I find it difficult to understand the positioning of the second image. See code snipped below, this is what I use:
CGRect innerButtonFrame = CGRectMake(self.center.x/2, self.center.y/2,innerButtonSelectedImage.size.width,innerButtonSelectedImage.size.height);
Taken from:
-(id) initWithImage:(UIImage *)image
{
if(self = [super initWithImage:image]){
//
self.userInteractionEnabled = true;
// Initialize gesture recognizers
UITapGestureRecognizer *tapInView = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapInImageView:)];
[self addGestureRecognizer:tapInView];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressInView:)];
[self addGestureRecognizer:longPress];
// Initialize labels
..
// Inner circle image
innerButtonView = [[UIImageView alloc] init];
innerButtonSelectedImage = [UIImage imageNamed:#"inner circle.png"];
CGRect innerButtonFrame = CGRectMake(self.center.x/2, self.center.y/2,innerButtonSelectedImage.size.width,innerButtonSelectedImage.size.height);
innerButtonView.frame = innerButtonFrame;
[innerButtonView setImage:innerButtonSelectedImage];
// Add additional ui components to view
[self addSubview:innerButtonView];
..
[self addSubview:descriptionLabel];
}
return self;
}
EDIT: This is how it looks like if I change the positioning code to the following:
CGRect innerButtonFrame = CGRectMake(0, 0,innerButtonSelectedImage.size.width,innerButtonSelectedImage.size.height);
innerButtonView.frame = innerButtonFrame;
I also don't understand why the image is bigger than the screen.. as the blue one should be 500x500 pixel wide and the screen of the iPhone 6 should be 1334 x 750.
How about:
CGRect innerButtonFrame = CGRectMake(0, 0, innerButtonSelectedImage.size.width,innerButtonSelectedImage.size.height);
innerButtonFrame.center = self.center;
If you need 500*500 circle then add the circle half means Replace 500*500 with 250*250 . And small circle replace 350*350 with 175*175 And solve your problem.
I hope your problem will solve..Enjoy
Thanks..
I'm writing something relatively simple, or so I thought.
Firstly, the code, for which I'm trying to place an image on the background of the UICollectionView if there are no results returned from my server. The image is 200 by 200:
UIView *myView = [[UIView alloc] initWithFrame:self.view.bounds];
CGRect myViewSpace = self.view.bounds;
CGFloat myX = (myViewSpace.size.width /2.0) - 100;
CGFloat myY = (myViewSpace.size.height /2.0) - 100;
UIImageView *imView = [[UIImageView alloc] initWithFrame:CGRectMake(myX, myY, 200, 200)];
imView.image = [UIImage imageNamed:#"imNotHome"];
[myView addSubview:imView];
myCollectionView.backgroundView = myView;
Once there are results, I want to be able to remove it.
I thought it'd be as simple as placing the following, before I reloaded the UICollectionView:
[myCollectionView.backgroundView removeFromSuperview];
However, it appears to be doing nothing.
Am I missing something?
Thanks in advance!
It should be done this way instead:
myCollectionView.backgroundView = nil;
Explanation: You should unset the UICollectionView's background in the same way as you set it. You didn't set it by manipulating the view hierarchy, but by setting the background property. You did call addSubview in the previous line, but that was to add a subview to your background view, not to add the background view itself.
Edit:
There is a very good article about this UICollectionView bug here:
http://blog.spacemanlabs.com/2013/11/uicollectionviews-backgroundview-property-is-horribly-broken/
The solution the author gives is to reset the background to an empty opaque view:
UIView *blankView = [UIView new];
blankView.backgroundColor = [UIColor whiteColor];
[myCollectionView.backgroundView removeFromSuperview];
myCollectionView.backgroundView = blankView;
Also, the author recommends not using the backgroundView property at all but doing it yourself:
Frankly, I think the best solution is to just ignore the backgroundView property all together. Instead, make the collection view’s background clear, and implement your own backgroundView; just throw a view behind the collection view.
I am adding a GLKView to a UIView within a UIViewController. Eventually I will split the code out to use a UIViewController and GLKViewController, but for now my hands are tied and need to use the UIViewController.
My issue is that when I add the GLKView to the UIVIew via the storyboard, everything works fine. When I try to add the GLKView programmatically it doesn't seem to get initialized quite right. It shows up grey.
The only difference in my implementation aside from using an IBOutlet is that I call the first and last lines in the code block below.
glview = [[GLKView alloc] init]; // Non storyboard
[glview setContext:context];
[glview setDelegate:self];
[glview setUserInteractionEnabled:YES];
[self.view addSubview:glview]; // Non storyboard
After the initialization I set up an AVCapture session. I set the frame on my GLKView in the controllers viewWillAppear function. Do you see any reason why my GLKView isn't getting initialized correctly?
The application is managing the drawing routine so I needed to disable the "enableSetNeedsDisplay" on my programmatically created GLKView. The following code fixed my issue. Take note of the the penultimate line.
glview = [[GLKView alloc] init];
[glview setContext:context];
[glview setDelegate:self];
[glview setUserInteractionEnabled:YES];
[glview setEnableSetNeedsDisplay:NO]; // HERE IS THE FIX
[self.view addSubview:glview];
I have a UIView subclass and I am drawing in the drawRect method.
How can I add an overlay image on top of the drawRect target layer?
I add the following in the initWithFrame method:
//add the image overlay
UIImageView *overlageImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"downloaderFront.png"]];
overlageImage.center = self.center;
[self.layer addSublayer:overlageImage.layer];
//replacing the above line with below line does not fix either
[self addSubview:overlageImage];
This image is not visible on top of the drawRect drawing.
The stuff with the layer is certainly wrong. Just do this:
UIImageView *overlageImage =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"downloaderFront.png"]];
[self addSubview:overlageImage];
You can play with the frame of overlageImage to get the position where you want it.
As to your actual question: put a breakpoint. I suspect you'll find that your code is never even running (because your view's initWithFrame is never called). That's just a guess - you didn't say anything about how this UIView subclass instance gets into the interface - but it's a common enough mistake, and one should always suspect the obvious first!
I would like to obtain this effect (http://stackoverflow.com/questions/7023271/how-to-adjust-drop-shadow-dynamically-during-an-uiimageview-rotation) but from a more complex image than just a red square ! If the link ever gets broken, it's a about how to adjust drop shadow dynamically during an UIImageView rotation.
So I tried implementing something but I just can't get the shadow in a separate layer... Here is my code, very simple, but doesn't work:
// here is my code
- (void)viewDidLoad {
[super viewDidLoad];
testView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"handNoShadow.png"]];
testViewShadow = [[UIView alloc] initWithFrame:testView.frame];
testViewShadow.layer.shadowPath = [[testView layer] shadowPath];
testViewShadow.layer.shadowOpacity = 1.0;
testViewShadow.layer.shadowOffset = CGSizeMake(10, 10);
testViewShadow.layer.shadowRadius = 5.0;
[self.view addSubview:testViewShadow];
[self.view addSubview:testView];
}
PS: i did #import
I do get an image but no shadow... =(
Any lead, help, code, link... is welcome !
Thanks
possible cause:
your testViewShadow.clipToBounds property is set to YES (should be NO)
your testViewShadow do the drawing of the shadow correctly but another UIView is on top and mask it. Check your Z order. Either the order in Storyboard/Nib file (or the order you added the subviews programmatically). Last in the list (or last one added) is on top. For my app I had to put the UIView that need a shadow last so that no other view mask it.