Custom UIButton, enabled property works but hidden property won't - iphone

I have a little problem on my Xcode project (iPhone app).
I just modified the rounded rect UIButton I used before to a custom one but now the hidden property isn't working anymore.
if (artwork)
{
artworkImage = [artwork imageWithSize: CGSizeMake (256, 256)];
shareButton.alpha = 1.0;
shareButton.enabled = YES;
//[shareButton setHidden:NO]; // Won't work, I don't know why
}
else
{
shareButton.alpha = 0.0;
shareButton.enabled = NO;
//[shareButton setHidden:YES]; // Won't work, I don't know why
}
I found a workaround, using enabled property and alpha instead of hidden.
But I would like to understand why the hidden property isn't working anymore.
Thanks for the answers.

Are you trying to animate the show/hide? I believe the hidden property is not animatable. See this question. A workaround in this case is to use the alpha property use the animation callback ([UIView setAnimationDidStopSelector:]) to set the visible state after the animation has been completed.

Try shareButton.hidden=YES;

Related

Can the border of UITextView or UILabel be set in Storyboard?

I want to set the border of a UITextView or a UILabel in a Storyboard.
Can it be done?
Programmatically, it is setBorderColor and setBorderWidth.
But can the border be set in a Storyboard?
As was previously pointed out, these properties are part of a layer, not part of a view. But you can still set their values in IB. As hypercrypt pointed out, you can use User Defined Runtime Attributes. Since all views have a "layer" property, you can set "layer.borderWidth" for instance.
Here's a case, where I'm changing the cornerRadius. Works great.
use simple code in .m,it show border in view
view.layer.cornerRadius = 5.0f;
view.layer.masksToBounds = NO;
view.layer.borderWidth = .5f;
view.layer.shadowColor = [UIColor orangeColor].CGColor;
view.layer.shadowOpacity = 0.4;
view.layer.shadowRadius = 5.0f;
If you're targeting iOS 6+ you can use the User Defined Runtime Attributes in the Identity Inspector to set any properties. Performance is not an issue for either, so it doesn't matter.

Image View in Button Doesn't Appear

I am trying to use the background view of the image view of a UIButton but for some reason it will not show up. This is what I have tried:
detailCell.greenPriorityButton.imageView.frame = detailCell.greenPriorityButton.frame;
[detailCell.greenPriorityButton.imageView setBackgroundColor:[UIColor greenColor]];
[detailCell.greenPriorityButton.imageView setHidden:NO];
[detailCell.greenPriorityButton.imageView setOpaque:YES];
I have called NSLog on the imageView property and it seems everything is as it should be. I can also tap on the button and the method associated with it will be called so I know it exists. Just in case I am missing something here is the NSLog return:
<UIImageView: 0x1d97e1b0; frame = (254 61; 20 20); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x1d97e210>>
The reason I am not using dot notation to set the image view properties above is because they don't seem to change the values. For example, if I say detailCell.greenPriorityButton.imageView.hidden = NO; it doesn't seem to do anything.
Thanks for any help!
Edit
The reason for not just setting the background color of the button and not its image view is because I am trying to create a small shape in the button. However I want the tappable space to have margins around the shape so it is still user friendly. I thought the image view property would lend useful as I could manipulate the frame and layer of that separately from the frame and layer of the button.
I have now tried adding a UIView *greenBackground as a subview to the button, however this doesn't appear either.
If you want a view that you can have within the view of the button for the purpose of setting its color then I would think that trying to use the imageview is the wrong approach. You could just add a subview that has the changed background color. something like this:
UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];//Whatever rect you want that will be in reference to the frame of the button
colorView.backgroundColor = [UIColor greenColor];
[detailCell.greenPriorityButton addSubview:colorView];

Text in UITextField moves up after editing (center while editing)

I have a strange problem. I have an UITextField in which the user should write the amount of something, so the field is called "amountField". Everything looks fine, when the user starts editing the textfield the text is in the vertical and horizontal center - that's great.
However, when the user ends editing the text moves up a little bit. I tried many things, nothing helped...
I am adding screenshots below, so you can see what is the problem.
This is what it looks like while editing the field - that's ok.
And this is how it looks when done editing - that is the problem!
Please, if anybody know what could cause this I would be very grateful! :)
Here is some of my code related to the amountField.
amountField.keyboardType = UIKeyboardTypeNumberPad;
amountField.returnKeyType = UIReturnKeyDone;
amountField.delegate = self;
[amountField setFont:[UIFont fontWithName:#"Nuptial Script LT Std" size:30]];
amountField.borderStyle = UITextBorderStyleNone;
UIImage *amountBg = [UIImage imageNamed:#"skin2_ipad_amountField.png"];
[amountField setBackground:amountBg];
amountField.rightView = nil;
//amountField.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2];
amountField.textAlignment = UITextAlignmentCenter;
amountField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
amountField.adjustsFontSizeToFitWidth = YES;
amountLabel.textColor = UIColorFromARGB(0x313030); //Using my own macro
amountField.frame = CGRectMake(300, 480, 136, 32);
amountField.center = CGPointMake(605, 439);
PS: Those white corners are there because I set the background to white with 0.2 alpha, that's ok.
I had a similar issue that started happening on iOS 9. Basically I have a UITextField in a collection view cell. Sometimes when the user is done typing and editing ends, the text "bounces" up then down again into its correct position. Very strange and annoying glitch. Simply making this tweak fixed the issue on iOS 9 and proved to be safe on iOS 7 and 8:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField layoutIfNeeded]; //Fixes iOS 9 text bounce glitch
//...other stuff
}
So...
After many hours of trying many things - I have found the problem.
In my case the problem is the font. I really don't know why, but the author of the font made the font weird (leading etc.), it has a blank space on the bottom. I don't know why, but when you are editing the text all of the text properties are ignored, but after you finish editing, they are applied.
So, if you have a similar problem, try changing the font to Arial or something similar.
For a full explanation, please consult these following links: link 1 and link 2. The solution recommended in these links can avoid you a lot of headaches and can even be applied to fix problem like text moving to the top when you start editing an UITextField (using System font or other particular fonts).
Disabling ClipsToBounds for the TextField solved it for me.
This bug happened to me when I set text & became the first responder in viewDidLoad or viewWillAppear. When I moved the becomeFirstResponder code to viewDidAppear the bug went away.
I'm struggling with this issue almost every time when the design of app is with custom font. One option is to fix the font (but this is too much work – at least for me :) ). The second option I'm using is subclassing the UITextField and overriding the editingRectForBounds: and placeholderRectForBounds: methods and correct the offset. It should work for your case too.
#implementation MyTextFieldWithFixedFontPosition
-(CGRect)editingRectForBounds:(CGRect)bounds{
return CGRectOffset([self textRectForBounds:bounds], 0, 0.5); //0.5 is just example, you can adjust to any offset you like
}
-(CGRect)placeholderRectForBounds:(CGRect)bounds{
return [self editingRectForBounds:bounds];
}
#end
I haven't tested it with leftView or rightView though, so be careful when using these :)
NOTE: this approach is "font dependant", values used for offset may vary for each font and size
There is a glitch on iOS 8.1 and below, I do not know if they will fix it later but at that time there is not an unique solution which fixes all cases, because the bug and the solutions are font's type, size dependent.
One of this solution or a combination of these solutions below can fix your problem:
Changing the font's size.
Changing the font's type.
Changing the UITextField's size.
Decompiling the font in question, modifying the font's characteristics and recompiling it (for more explanation please consult the following links: link 1 and link 2).
Otherwise this other self-sufficient solution below can fix your problem:
Swift version
import UIKit
class CustomTextField: UITextField {
...
override func textRectForBounds(bounds: CGRect) -> CGRect {
// Possible values.
return CGRectInset(bounds, CGFloat(35.0), CGFloat(0.0))
}
override func editingRectForBounds(bounds: CGRect) -> CGRect {
// Possible values.
return CGRectInset(bounds, CGFloat(35.0), CGFloat(0.0))
}
override func placeholderRectForBounds(bounds: CGRect) -> CGRect {
// Possible values.
return CGRectInset(bounds, CGFloat(35.0), CGFloat(0.0))
}
}
This solution has been tested with leftView and works like a charm.
NOTE: this approach is "font dependant", values used for CGRectInset may vary for each font and size.
I fixed this by adding height constraints to my UITextFields.
I wasn't able to change the font file, so when I solved this I saved the original UITextField's frame in a property and applied the following code:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.frame = self.usernameFrame;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
textField.frame = CGRectOffset(self.usernameFrame, 0, 1);
}
It's a bit hacky, but it gets the job done.
I had similar issues with a UITextfield embedded in a UITableViewCell. Where exactly is this code located in your project? What I believe is happening is that after you've finished editing a particular textfield, it sends itself -setNeedsDisplay and its drawRect: is subsequently called. This might explain the shift in alignment. In my particular scenario, I had to use a table view delegate method -willDisplayCell... to set the content alignment. I would have to know more about your design to possibly offer a suggestion.
One potential solution would be to use the text field delegate methods to set the content alignment.
-(void)textFieldDidEndEditing:(UITextField *)textField{
if (amountField == textField){
amountField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
}
}
Check the keyboard to change the view of the position of the pop-up if there is line of code self.view.layoutIfNeeded() Delete it.Good Luck!
This is because the BaselineOffset for the textfield got changed.
In UITextFieldDidEndEditing creating an attributed text with NSBaselineOffset: 0 and using that attributedText would fix the problem.
-(IBAction)txtFieldDidEndEditing:(UITextField *)sender {
NSDictionary *style = #{
NSBaselineOffsetAttributeName: #(0)
};
self.txtField.attributedText = [[NSAttributedString alloc] initWithString:self.txtField.text attributes:style];
}
These solution above doesn't work for me.My solution is subclass UITextField and override setText:
- (void) setText:(NSString *)text {
[super setText:text];
[self layoutIfNeeded];
}
I used this extension. Only problem was I didn't add translatesAutoresizingMaskIntoConstraints = true before I called it. Ah silly goose
func centerVertically() {
textContainerInset = UIEdgeInsets.zero
textContainer.lineFragmentPadding = 5
let fittingSize = CGSize(width: bounds.width, height:
CGFloat.greatestFiniteMagnitude)
let size = sizeThatFits(fittingSize)
let topOffset = (bounds.size.height - size.height * zoomScale) / 2
let positiveTopOffset = max(1, topOffset)
contentOffset.y = -positiveTopOffset
}

UIView.setalpha is not working?

I have created a bar to appear over the keyboard for next/previous/done like the safari browser. However, the setalpha property of the UIview of the bar doesn't seem to be working. No matter what value I set it to, nothing changes. Here is the code...
here is where the create view is called..
-(void)textFieldDidBeginEditing:(UITextField *)textField{
// Call the createInputAccessoryView method we created earlier.
// By doing that we will prepare the inputAccView.
[self createInputAccessoryView];
// Now add the view as an input accessory view to the selected textfield.
[textField setInputAccessoryView:inputAccView];
// Set the active field. We' ll need that if we want to move properly
// between our textfields.
txtActiveField = textField;
}
here is where i actually create and set its values...
-(void)createInputAccessoryView{
// Create the view that will play the part of the input accessory view.
// Note that the frame width (third value in the CGRectMake method)
// should change accordingly in landscape orientation. But we don’t care
// about that now.
inputAccView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 310.0, 40.0)];
// Set the view’s background color. We’ ll set it here to gray. Use any color you want.
[inputAccView setBackgroundColor:[UIColor darkGrayColor]];
// We can play a little with transparency as well using the Alpha property. Normally
// you can leave it unchanged.
[inputAccView setAlpha: 0.1f];
... code for adding buttons and their properties
}
so basically this is all there is to it. but the setAlpha property does nothing, regardless of what I set it to. However, background color works fine. Any ideas?
Thanks
If you want it to look like the prev/next/done bar in Safari etc, create a UIToolbar with the style set to translucent black. This also has the advantage of laying out your buttons nicely, with the correct style, and auto adjusting between landscape and portrait.
Not a direct answer to your question but probably a better way of acheiving what you actually want.
out of utter curiosity try using:
UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 310.0, 40.0)];
overlay.backgroundColor = [UIColor blackColor];
overlay.alpha = 0.5f;
[self.view addSubview:overlay];
it's hard to tell what the issue is without being able to see how many views you have and how you've stacked them. check you've defined your "addSubview" and what the hierarchy looks like

setting a default value for slider when app loads

In my iPhone app I'm using a slider for adjusting the volume and working fine.
But I'm not able to set a default value for slider when app loads.
I have seen that only when the slider is touched the value gets changed.But i need a default value when app loads.
My code is given below
- (IBAction)sliderChanged:(id)sender {
slider = (UISlider *) sender;
slider.minimumValue = 0.5;
slider.maximumValue = 2.2;
progressAsInt = slider.value ;
}
how can i solve this issue.
please help.Thanks in advance.
There are two ways to change default value of slider.
Using Interface builder. See attached picture and value of current property.
Using Code
You just need to write this one line in viewWillAppear delegate method.
-(void)viewWillAppear:(BOOL)animated{
slider.value = 1.0;
}
Hope this help.
First you'll need to set the slider to an IBOutlet iVar of the view controller where the slider is.
Then, in the viewDidLoad of that view controller, you can set the value you want.
(Also, you should be able to do it in Interface Builder, but I'm not very familiar with it).