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.
Related
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.
I have looked on google and stack flow, and although I have found similar responses, I am still no more wiser then before I set out my hunt for the solution to this problem.
I have one button, that each time pressed I want it to show the next image.
I believe I have to set up an array of all of the images I have in my project to do this, then maybe something like a counter for the button so it knows which image to show in the UIImageView.
So the project is something like this:
1 x view controller, with 1 x UIImageView, 1 x button, and say 5 images.
UIImageView takes up the whole screen, image 1 displayed in UIImageView, button pressed, displays image 2, button pressed again displays image 3, and so on until you get to image 5, once at image 5, if you press the button again, it will go back to the start (image 1).
I am so new that I would like some example code to help please, not to copy and paste but to study to see how it works, and how I can implement similar within my app.
I hope this makes sense. I'm not up with the lingo yet, so idiot proof advise would be good.
Ok I just whipped up some example for you here
Define in .h
UIButton *b;
UIImageView *i;
NSArray *a;
int count;
and write these in .m
- (void)viewDidLoad
{
[super viewDidLoad];
b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(0, 0, 100 ,100);
[b addTarget:self action:#selector(changeImage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:b];
i = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 100, 100)];
[self.view addSubview:i];
a = [NSArray array];
//init array with images what you have
count = 0;
[i setImage:[a objectAtIndex:count]];
}
-(void) changeImage{
count = ++count%5;
[i setImage:[a objectAtIndex:count]];
}
Do not follow my naming convention, it was an example. You should write it out nicely.
This should work once you put in the images. Let me know if you have a question but please do google before asking anything.
In objective C, one can set a background image to a stretched png like so:
button = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
[button setTitle: #"Tap me" forState: UIControlStateNormal];
[button setBackgroundImage:[[UIImage imageNamed: #"greenButton.png"]
stretchableImageWithLeftCapWidth:8.0f
topCapHeight:0.0f]
forState:UIControlStateNormal];
Trying to transpose this over to Ruby, I keep getting exceptions though. The problem is with the two methods called on the UIImage instance: stretchableImageWithLeftCapWidth and topCapHeight.
I've tried the following to no avail:
greenImage = UIImage.imageNamed("greenButton.png")
greenImage.stretchableImageWithLeftCapWidth = 8.0
greenImage.topCapHeight = 0.0
#timerButton.setBackgroundImage(greenImage, forState: UIControlStateNormal)
Can anyone advise?
You have incorrectly broken that method selector up
It is declared as
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
it should be called like this
greenImage.stretchableImageWithLeftCapWidth(8.0, topCapHeight:0.0)
You'll most likely want to assign that to something so it might look like this
greenImage = UIImage.imageNamed("greenButton.png")
greenImage = greenImage.stretchableImageWithLeftCapWidth(8.0, topCapHeight:0.0)
#timerButton.setBackgroundImage(greenImage, forState: UIControlStateNormal)
Side note
You are correct the method is marked as newly deprecated in iOS 5, but it's also important to note that the replacement method was also introduced in iOS 5 so if you plan to support older iOS's then you will need to continue using this.
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;
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"]];