UIImageView places on UIButton - objective-c

I want to place an UIImageView on UIButton programmatically (with constraints). The UIButton is implemented from Storyboard. I don´t find example from the net. I tried this code:
//Test Image button
UIImageView* iqImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"iq1"]];
iqImageView.frame = CGRectMake(0, 0, self.iqCategoryButton.frame.size.width, self.iqCategoryButton.frame.size.height);
iqImageView.contentMode = UIViewContentModeRedraw;
[self.iqCategoryButton addSubview:iqImageView];
The problem is at this moment, the frame´s size of the button is not equal as it will be seen on the screen (due to constraints).

Related

Using an LSR image on a UIButton

I am creating a tvOS app and I want to use parallax images on a couple of buttons. From the docs:
To incorporate parallax images in your app:
Create a UIImage object.
You load the image differently depending on whether the image is included in your app bundle or whether you have downloaded the
image.
Bundle—Load images using imageNamed:.
Downloaded file—Load images using imageWithContentsOfFile:.
Create a new UIImageView object using the loaded images.
If the UIImageView is part of another view, set adjustsImageWhenAncestorFocused to YES on the UIImageView.
I know it says UIImageView there, but I was hoping to make the same effect happen on a UIButton, like the home screen app icons.
I've created the artwork, made a stack in the asset catalog, and loaded the image with imageNamed:, but the UIButton does not behave like a parallax image. It does not sway around like the home-screen icons do. It just looks like a flat image.
Is there something else I have to enable in order for the UIButton to behave like the home screen app icons?
UIButton* quitGame = [[UIButton alloc] initWithFrame:rectWithNewX(playAgain.frame, 985)];
[quitGame setImage:[UIImage imageNamed:#"quit.lsr"] forState:UIControlStateNormal];
[quitGame setAdjustsImageWhenHighlighted:YES];
fadeIn(quitGame, self.view, 0.5);
As of right now, this is not possible with just UIButtons, however, I did find a workaround.
Instead, create a UIImageView that will fill the size of the UIButton and make it a subview of the UIButton. Then call the adjustsImageWhenAncestorFocused: and set it to true. Voila!
UIButton* playAgain = [[UIButton alloc] initWithFrame:CGRectMake(centerX(650, self.view), sh() - 250, 300, 180)];
[playAgain setAdjustsImageWhenHighlighted:YES];
[playAgain addTarget:self action:#selector(playAgain) forControlEvents:UIControlEventPrimaryActionTriggered];
fadeIn(playAgain, self.view, 0.5);
UIImageView* playAgainIGV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 180)];
[playAgainIGV setImage:[UIImage imageNamed:#"playagain.lsr"]];
[playAgainIGV setAdjustsImageWhenAncestorFocused:YES];
[playAgain addSubview:playAgainIGV];
I've had issues when setting files or precompiled LSRs.
Creating the LSR in XCode Asset Catalogue through Asset Catalogue -> New Apple TV Image Stack and dragging in the PNGs, then setting the image either through Interface Builder or through this works:
-(void) setLsr:(UIButton *)button lsrNamed:(NSString *)lsr {
[button setAdjustsImageWhenHighlighted:YES];
UIImageView *biv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, button.frame.size.width, button.frame.size.height)];
[biv setImage:[UIImage imageNamed:lsr]];
[biv setAdjustsImageWhenAncestorFocused:YES];
[button addSubview:biv];
}
Regarding the answer of david I made a simple Swift method to create a parallax effect button:
func createParallaxButton(button: UIButton, imageNamed: String) {
button.adjustsImageWhenHighlighted = true
let buttonBg = UIImageView(image: UIImage(named: imageNamed))
buttonBg.adjustsImageWhenAncestorFocused = true
buttonBg.frame = button.bounds
button.addSubview(buttonBg)
}
createParallaxButton(myButton, imageNamed: "myButtonimage.lsr")

UIButton not showing up

I am trying to create a UIButton programmatically instead of using the interface builder. I initialize and set the button frame but the button doesn't seem to appear on the view. My code is as follows:
UIButton *showInfoButton = [[UIButton alloc] init];
CGRect buttonFrame = CGRectMake(0,0, 150, 150);
showInfoButton.frame = buttonFrame;
What am I missing? Please bear with me as I am new to iOS.
You forgot to add your UIButton as a subview to the UIView you want it to display on. Since you are building UI programmatically, you need to manually add the button as a subview to the container view.
You can do so by using this line of code:
[self.view addSubview:showInfoButton];
The button might not still appear as you are making a custom button which has a transparent background colour. In order to see the button, you can set the UIButton's backgroundColor property as follows:
[showInfoButton setBackgroundColor:[UIColor blackColor]];

Set order on displaying paths/images

I have a couple of arcs and rects in a UIView, and a UIImageView as well. I want to have the UIImageView in the background. I have ordered it in IB so that the image view is behind my labels and buttons.
First, I need to put an image in the image view, which I don't want to do in IB. I've tried this code
UIImage *myImage = [UIImage imageNamed:#"image.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:#"myImage"];
But it doesn't work. After I get that working, what can I do to move my paths in front of the image view?
I think you want to draw to put a background image and on top of it want to draw circles(arcs).
If my understanding is correct. First draw the image on to the view using CGContextDrawImage() and then draw arcs on top of it. (No need to use image view).
Add image to UIImageView :
UIImage *myImage = [UIImage imageNamed:#"image.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:myImageView];

UIScrollView with background image

I want to have a background image behind a UIScrollView area. That part's easy. The issue I'm running into is I don't want the image to move. I want it to fill the screen and then the scrolling just happens over that without effecting the image in anyway.
I've looked through lots of other postings on similar topics, but can't seem to get anything to work.
When I do the following, the background scrolls:
CGRect fullScreenRect = [[UIScreen mainScreen] applicationFrame];
UISCrollView *scrollView = [[UIScrollView alloc] initWithFrame:fullScreenRect];
scrollView.backgroundColor = [UIColor clearColor];
UIImageView *bkg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"img"]];
[scrollView addSubview:bkg];
Put the image in a UIImageView and place it behind the UIScrollView. Set the UIScrollView's color to 100% transparency ([UIColor clearColor]).

Objective C - change arrangement of UIImageview

I have created a UIImageView in my code:
UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(150, 60, 180, 180)];
[self.view addSubview:background];
and I have also got a UIImageView in my XIB. The programmatically-created UIImageView appears on top of the XIB's UIImageView. How do I change this so the XIB's UIImageView appears on top?
Generally I would put my background directly in the nib file. But to add it programmatically, use insertSubview:belowSubview:, insertSubview:atIndex:, or sendSubviewToBack: to put it where you want it.