How to center a UIImage to UIButton? - uiimage

I want to know how to center a UIImage to UIButton. I am using the setImage method of UIButton to place the UIImage..

You can use the contentMode Property:
button.imageView.contentMode = UIViewContentModeCenter;

Remove the text from the UIButton before setting the content mode

Sometimes you might have the wrong content alignment. Make sure they are both in the center with :
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

Related

Want to change UIbutton to an image of my choice

UIButton *openButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
openButton.frame = CGRectMake((main.frame.size.width/2)-15, (main.frame.size.height/2)+75, 30, 30);
openButton.backgroundColor = [UIColor clearColor];
openButton.layer.cornerRadius = 16;
openButton.layer.borderWidth = 2;
openButton.layer.borderColor = themeColor.CGColor;
[openButton addTarget:self action:#selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
[openButton addTarget:self action:#selector(showMenu)
forControlEvents:UIControlEventTouchDownRepeat];
This is my open button's code that was already implemented into the mod menu but I really want to change it to a sample image instead of a circle. I tried changing it to an UIImage but I am not sure how to do it properly since i am new to coding.
Please try this:
yourBtn.setImage(UIImage(named: "ImgName"), for: .normal)
Please convert to objective c. setImage is a property of UIButton for image setting.
It may helps you. Thank you
A part from question:
i really want to change it to a sample image instead of a circle
seems like a problem with:
openButton.layer.cornerRadius = 16;
Removing this line should give you actual image on button.

Xcode 5, why isn't the image resizing?

Hello I am trying to resize a UIImage, but even though I'm not getting any errors it is not working.
hers the code of .h file
IBOutlet UIImageView *Fish;
heres the code of .m file
Fish.frame = CGRectMake(0, 0, 300, 293);
What am I doing wrong?
Thanks for any help
The image is probably not resizing because you are just resizing the image view. Make sure in your storyboard that you make the image view (Fish), have the move ScaleToFill. I can't do screenshot due to reputation ( sorry :( )
Alternately, if your goal is not to resize the image view but to resize the image it is holding, you can do this:
UIImage *image = Fish.image;
UIImage *image = YourImageView.image;
UIImage *tempImage = nil;
CGSize targetSize = CGSizeMake(80,60);
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectMake(0, 0, 0, 0);
thumbnailRect.origin = CGPointMake(0.0,0.0);
thumbnailRect.size.width = targetSize.width;
thumbnailRect.size.height = targetSize.height;
[image drawInRect:thumbnailRect];
tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
YourImageView.image = tempImage;
and you would set thumbnailRect to whatever size you want.
Hope this helps! Please search Nerdy Lime on the app store to find all of my apps! Thanks!
I bet your outlet is not hooked up. In your "viewDidLoad" method, try doing this:
if(Fish)
{
Fish.frame = CGRectMake(0, 0, 300, 293);
} else {
NSLog(#"Fish is null; why did I forget to connect the outlet in my storyboard or xib?");
}
And this isn't the best way to resize your UIImageView. If you're using regular springs & struts, you can grow an outlet by clicking the springs & struts to grow based on the superview's size, e.g.:
And if you're doing AutoLayout, there's a different thing you can do (basically pin your view to all four sides of the superview).
Here is how I do it:
1) select the outlet / object you want to add constraints to (in your case, it'll be the fish image view)
2) see the segmented control at the bottom of the Interface Builder window? Click on the second one and you'll see a popover view open up with a list of possible constraints to add.
3) In my example, I'm adding constraints in my ImageView to always be 10 pixels from each edge of the superview (note the four "10"s and solid red lines meaning I'm adding four constraints).
AutoLayout is a pain to get accustomed to (and I'm still learning it myself), but I suspect that once one gets the hang of it, it'll be a powerful new tool especially as Apple brings in additional iOS screen sizes in the very near future.

Align UIImage in UITableViewCell

I'm trying to align a UIImageView to the left of a UITableViewCell.
I thought I have to use something like
cell.imageView.image = [UIImageView CGFloat....];
but as it turns out that it doesn't work.
Does anyone have an idea?
This might work:
cell.imageView.contentMode = UIViewContentModeLeft;

iphone ipad change text field colour

please, how can I change the color of the text field background and the colour of place holder?
I saw this answer
here in stack, change bkgrnd
but Is not clear where to call this
[UIColor colorWithRed:0.2f green:0.3f blue:0.4f alpha:0.50001f];
shall I override the UITextField.h? where?
or where shall I change the colour?
and for the changing of the place holder, but it doesn't work!
stack, change holder.
You can overwrite UITextField and do this in the init method. But I would highly recommend to do simply after you created your UITextField
textField.backgroundColor = [UIColor colorWithRed:0.2f green:0.3f blue:0.4f alpha:0.50001f];
To change the background of your textfield :
yourTextField.backgroundColor = [UIColor yellowColor];
To change the placeholder text color :
[yourTextField setValue:[UIColor blackColor] forKeyPath:#"_placeholderLabel.textColor"];

How do I set the background of UIScrollView to be transparent?

What i ultimately would LOVE to do is having my scroll view contents scrolling (in the scroll view control), and have a static background (a wallpaper image).
I've tried a combination of things, none of which really yields what im looking for, not even close really.
Has anybody attempted this before?
This is actually very easy, a UIScrollView inherits from a UIView so you just need to set the backgroundColor property:
aScrollView.backgroundColor = [UIColor clearColor];
The above line of code sets the background to transparent so you can see through to whatever you want to put behind. Note also that you should do the same thing to the sub-views that you put inside the scroll-view - those views should also have a transparent background colour.
If you want a static image, use a pattern as in the line of code below. This can be a texture image smaller than the screen that will be repeated/tiled, or a full-screen image so you don't see any repeats.
aScrollView.backgroundColor = [UIColor colorWithPatternImage:anImage];
For those not having clear answer to this. It goes:
put an dummy holder imageview to the mainview and set it to the desired background image or set its backcolor as pattern, then inside this view put the scrollview and set its background to transparent.
Then you get the desired effect as with Safari on iPad for example on page scroll.
This is a code for Monotouch, but is straightforward to understand.
this.View.BackgroundColor = UIColor.FromPatternImage (UIImage.FromFile ("Images/About-bg0.jpg"));
scrollView = new UIScrollView (new RectangleF (0, 0, 320, 450));
scrollView.ContentSize = new SizeF (320, 640 + 508 + 1000);
//scrollView.BackgroundColor = UIColor.FromPatternImage (UIImage.FromFile ("Images/About-bg0.jpg"));
scrollView.BackgroundColor = UIColor.Clear;
After some quick googleing I found two links that will help you:
Google Groups discussion on this question
Blog post on how to do this.
Have you tried this,
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"content.png"]];
This one worked for me...
self.scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"tileableImage.png"]];

Resources