UIButton setting frame not working? - objective-c

I have written the following code for my app:
self.PlayButton = [[UIButton alloc] init];
//Setup Play Button
[self.PlayButton setTranslatesAutoresizingMaskIntoConstraints:NO];
self.PlayButton.frame = CGRectMake(self.view.frame.size.width * 0.38, self.view.frame.size.height * 0.666, self.view.frame.size.width * 0.48, self.view.frame.size.height * 0.29);
[self.PlayButton setBackgroundImage:[UIImage imageNamed:#"PlayButton.png"] forState:UIControlStateNormal];
[self.PlayButton.layer setMagnificationFilter:kCAFilterNearest];
[self.PlayButton addTarget:self action:#selector(PlayButtonMethod) forControlEvents:(UIControlEvents)UIControlEventTouchUpInside]
[self.view addSubview:self.PlayButton];
But when this code it run rather than the image appearing in a rather specific location it simply appears in the top left hand corner of the view. Almost like it had been set
self.PlayButton.frame = CGRectMake(0, 0, self.view.frame.size.width * 0.48, self.view.frame.size.height * 0.29);
It's odd because the width and height is put in correctly but for whatever reason the position of the button set by the CGRectMake is not taken into account. Ive done a bit of research into creating UIButtons in code and fro what I've seen not only has this happened to no-one else the code have written is perfectly liable.
Any help would be appreciated.
Thanks

Looks like ive found the fix:
Removing this line of code fixed the issue for me.
[self.PlayButton setTranslatesAutoresizingMaskIntoConstraints:NO];
not 100% sure why this was causing the problem but it fixed it.

When creating UIButtons using the standard alloc init method doesn't actually create a button. The method for creating and initializing a UIButton is called buttonWithType:. This creates a UIButton of the specified type if you use alloc init it will not work correctly, see the Apple Documentation for UIButton.
So you need to change line
self.PlayButton = [[UIButton alloc] init];
to
self.PlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
buttonWithType: allows you to pass in an enum of UIButtonType so it will accept any of the following values :
UIButtonTypeCustom
UIButtonTypeSystem
UIButtonTypeDetailDisclosure
UIButtonTypeInfoLight
UIButtonTypeInfoDark
UIButtonTypeContactAdd
UIButtonTypeRoundedRect
If you don't pass it a UIButtonType the button will not be initialized as it will not know what type of button you want and it will not assume. Also check out the Apple Documentation for UIButtonType
Side Note
I'm just going to include this as a side note. Also have a read of the Apple Coding Conventions Documentation as I have noticed that you are using uppercase to start your variable names (i.e. PlayButton). Variable names should start with lowercase (i.e. playButton) and Class and Enum names should start with uppercase (i.e. UIViewController). It's always good to stick to conventions as it makes you code more readable and maintainable so if another developer comes to modify your code it is easy for them or even yourself to read.

Related

Trying to add UIButton on top of vrf-reader ContentView

I am trying to insert some buttons in the PDF page rendered by the open source iOs reader vfr-reader https://github.com/vfr/Reader
Basically this is the code I am trying to implement in the initWithFrame method of ReaderContentView:
if (theContentView != nil) // Must have a valid and initialized content view
{
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setFrame:CGRectMake(269, 262, 100, 100)];
[myButton addTarget:self action:#selector(links) forControlEvents:UIControlEventTouchUpInside];
[myButton setTitle:#"asdasd" forState:UIControlStateNormal];
[theContentView addSubview:myButton];
The button doesn't work, and I can see that it is rendered and flattened in some way so it doesn't seem to be a button anymore as you can see here at full zoom.
However running DCInspector, despite warning that
2013-05-24 12:27:34.560 Reader[45333:c07] DCIntrospect: *** WARNING: One or more values of this view's frame are non-integer values. This view will likely look blurry. ***
seems to identify the button properly when inspected.
To test this case, just download the reader project and put my code in ReaderContentView at line 113. I am pretty sure I am doing something totally wrong. Thank you for help!
it was a bit tricky but I discovered that userInteractionEnabled was not set properly on the container view. I should have supposed that :)

Changing the size of UIStepper

I can't seem to change the size of UIStepper:
In IB, the Width and Height boxes are grayed out.
I used initWithFrame:
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(300, 638, 120, 80)];
But it does not change the size. Several posts on SO seemed to implied it is changeable. Any suggestion?
UIStepper* s = [UIStepper alloc] init];
s.transform = CGAffineTransformMakeScale(0.75, 0.75);
You can properly update the UIStepper size without transformation.
Use the following method to set the background image and the stepper will draw itself using the background size:
- (void)setBackgroundImage:(UIImage*)image forState:(UIControlState)state
Example
[self.stepper1 setIncrementImage:[UIImage imageNamed:#"plusIcon1.png"] forState:UIControlStateNormal];
[self.stepper1 setDecrementImage:[UIImage imageNamed:#"minusIcon1.png"] forState:UIControlStateNormal];
[self.stepper1 setBackgroundImage:[UIImage imageNamed:#"stepperBkg1.png"] forState:UIControlStateNormal];
[self.stepper1 setBackgroundImage:[UIImage imageNamed:#"stepperBkgHighlighted1.png"] forState:UIControlStateHighlighted];
[self.stepper1 setBackgroundImage:[UIImage imageNamed:#"stepperBkgDisabled1.png"] forState:UIControlStateDisabled];
This yields the following result on the left, compared to an unmodified stepper on the right:
stepperBkg1#2x.png:
stepperBkgHighlighted1#2x.png:
I tried the transform on my stepper - it did change the appearance and did scale it, however, the images of the + and - were stretched (so you have to scale in proportion to the original stepper.
Also, be careful because the area of touch that actually increments and decrements, does change - so on the stretched image, the button would not decrement along the entire view - so this is probably not a good solution....
from the doc:
The bounding rectangle for a stepper matches that of a UISwitch object.
Doesn't sound, like it is possible upfront.
Also in this blog post:
// Frame defines location, size values are ignored
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(120, 20, 0, 0)];
But you can try to transform it's layer.
You can provably scale it:
stepper.transform = CGAffineTransformMakeScale(1.75, 1.0);
I've made a small custom UIStepper class for it. No images needed, no transformation needed. Images generated automatically.
https://github.com/alelipona/VZCustomSizeStepper
Yes, you can change size of stepper.
first, right click on storyboard --> select (open as)--> Select (Source Code)
then find stepper in the code--> find width=??? and change.
then click on storyboard again and select open as interface builder.

Creating/Displaying Interface Objects with Code for the iPhone [Objective-C]

I'm currently designing an app that calls for dynamically creating objects and then displaying them in a ViewController. My problem is wading through the documentation to try and create the objects I need. I've done some searching and even tried to just sit down and figure it out, but alas, I've got nothing to show for it. I already know how to dynamically create the ViewController but I can't seem to get any objects in it.
Does anyone know how I could go about creating objects (Say a button and a slider) dynamically with Objective-C in XCode?
Creating a button in code is simple
- (void)addButton {
// Create button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Set the frame (location and size)
[button setFrame:CGRectMake(0, 0, 40, 30)];
// Set what happens when you touch the button
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
// Set button text
[button.titleLabel setText:#"Button"];
// Add button to view
[self.view addSubview:button];
}
A lot of interface elements follow a similar patter- alloc and init the object, set the frame and add it to a view.

UITextView setTextColor changes layout in UITableViewCell

I am trying to use the solution on the following page:
UITextView highlightedTextColor or similar option?
However, I am finding that when I call setTextColor on my UITextView and set the color to anything other than Color blackColor, the content in the UITextView appears shifted, and the color I do set it to doesn't take.
Playing around, I can repeat this behaviour by modifying the following initialization code in my UITableViewCell:
_notesTextView = [[UITextView alloc] initWithFrame:CGRectZero];
[_notesTextView setFont:[UIFont systemFontOfSize:12.0]];
[_notesTextView setTextColor:[UIColor redColor]];
[_notesTextView setUserInteractionEnabled:NO];
[self.contentView addSubview:_notesTextView];
The code above will cause the textView to display offset from what I expect, as compared to when I leave the color defaulted or set to blackColor, and the text doesn't show as red either.
This is so weird - any idea what could be wrong?
Do Not Use CGRectZero as it initiates the frame to some value you might know depending upon situation so if you have created a custom UITableViewCell then just set the frames in init or
-(void)layoutSubViews
{
}
I've confirmed that the textColor does indeed shift the text position (up and down for me). I've also tried it with initWithFrame(50, 20, 250, 31), and still experience the same problem.
What I've discovered fixes this issue is using the property textAlignment and setting it to one of the values: UITextAlignmentCenter, UITextAlignmentLeft, UITextAlignmentRight.

Adding a sub-view appears to do nothing

In my application, when the user clicks an infoButton, it should add another view to the screen at a specific location. I'm trying to achieve this behavior with the following method:
- (IBAction)showInfo1:(id)sender
{
UIView *myView1 = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)];
[self.view addSubview:myView1];
}
(I declared everything in the header file of my class.)
When I run the code and press the button, nothing appears to happen (I don't see the new view).
I also noticed that XCode is displaying the following warning:
Local declaration of 'myView1' hides instance variable.
Does anyone have any ideas?
How do you know nothing changes?
It looks like myView1 doesn't actually contain anything. Try setting the background color of myView1
- (IBAction) showInfo1: (id) sender
{
UIView *myView1 = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)];
myView1.backgroundColor = [UIColor redColor];
[self.view addSubview: myView1];
}
To open it at a specific point you need to change the parameters in CGRectMake() for example
to open it at the very top left with a width of 50 and height of 20 you would do:
CGRectMake(0, 0, 50, 20)
"Local declaration of 'myView1' hides instance variable." message appears because you have declared in your class some property with the same name (even if the type is different).
If you put UIView *myView1 in your class definition, your method would look like
- (IBAction)showInfo1:(id)sender{
myView1 = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)];
[self.view addSubview:myView1];
}
Here comes more considerations: you must to release myView1 when you stop using it, avoid to be placed more than once, etc. but here we have the basic idea.
Finally, maybe your view is already added, but 'cause it doesn't contains anything yet, you don't notice it. Also you would like to check UIViewController to see if works better for you.