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.
Related
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.
I'm using setBackgroundImage:forToolbarPosition:metrics: method of UIToolbar.
That's my code:
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320, 44)];
[toolbar setBackgroundImage:[UIImage imageNamed:#"top"] forToolbarPosition:UIToolbarPositionTop barMetrics:UIBarMetricsDefault];
[toolbar setBackgroundImage:[UIImage imageNamed:#"bottom"] forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
[self.view addSubview:toolbar];
As you can see I've put my toolbar on top. However when I run the code the toolbar image used is [UIImage imageNamed:#"bottom"].
How UIToolbarPosition is obtained?
My first idea is frame check ( < 1/2 superview frame is top, otherwise is bottom... or sort of).
It still on bottom. Any idea?
on iPhone UIToolbarPosition is always on bottom because UIToolbar is not supposed to be on top.
on iPad it works fine, UIToolbarPositionTop is used when toolbar.frame.origin.y = 0, UIToolbarPositionBottom otherwise.
I'm working on an iPad application. On the main screen there are 6 buttons which was created in IB. I'd like to arrange them dynamically from code, but I can't.
Already tried setting the frame/center of the objects, but they didn't move.
I have an array which holds the button outlets, and arrange them in a for loop.
Is there any suggestion?
thanks!
edit:
Here's some code(not the whole, but the rest is irrelevant now), how I loop through the buttons. Button outlets already set properly. The title of the buttons changes properly, also I can hide/show the buttons, but not the position.
If I'll just try some dummy code like:
tempBtn.frame = CGRectMake(100.0, 200.0, 60.0, 40.0);
nothing changes(but I suspect all the 6 buttons should be at the same position)
// I have this array, which contains the button outlets
tAddBtnArray = [NSArray ArrayWithObjects:tAddBtn1,tAddBtn2,tAddBtn3,tAddBtn4,tAddBtn5,tAddBtn6, nil];
//I loop through the array, set button title, and trying to change the y position
for(int i=0;i<6;i++){
UIButton *tempBtn = [tAddBtnArray objectAtIndex:i];
[tempField setText:#"Set some title"];
}
You can add uibbutons programatically in this way:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(yourMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Jesus Gil y Gil" forState:UIControlStateNormal];
button.frame = CGRectMake(100.0, 200.0, 60.0, 40.0);
[view addSubview:button];
I bet I know what it is. Do you have autolayout enabled on the xib file? If so, the frame is being set, and then the constraints are being evaluated, which move the buttons back to where they were assigned.
You have two options:
Turn off autolayout, so that you can manage the frames yourself.
Leave autolayout on, but update the constraints as well as the frame.
I would suggest turning autolayout off, but I'm just not particularly fond of it, so it is just a preference.
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.
I am using the following to create an NSPopupButton programmatically:
...
NSPopUpButton *accessoryView = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 24) pullsDown:YES];
NSFont *aFont = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]];
[accessoryView setBezelStyle:NSRecessedBezelStyle];
[accessoryView setFont:aFont];
[accessoryView setShowsBorderOnlyWhileMouseInside:YES];
[accessoryView setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];
NSArray *popupItems = [[NSArray alloc] initWithObjects:#"Your Account", #"Sign In", #"Create Account", nil];
[accessoryView addItemsWithTitles:popupItems];
...
Now, when I add the NSPopUpButton to my view, I end up with the button's text overlapping the icon used for the dropdown menu. I have seen this previously when I use NSControl:setAlignment but I am not using this here. Here is the output:
Can anyone see what I'm doing wrong?
Take care,
Jeremy
It just so happens this is a simple fix. (Thanks Beelsebob on irc.freenode.net!) Basically, you need to have the following code:
[[accessoryView cell] setArrowPosition:NSPopUpArrowAtBottom];
in there somewhere. (I added it just below the line to add the menu items.) I had read the API docs on this call a few times before, since I had implemented the same call with a value of NSPopUpNoArrow to remove the arrow as an interim fix, but it didn't make it clear that the proper value being used above would do what it's doing. Problem solved.