It seems that [UILabel appearance] doesn't work anymore with latest iOS SDK.
Let's take this as an example:
- (void)viewDidLoad {
[super viewDidLoad];
[[UILabel appearance] setBackgroundColor:[UIColor redColor]];
[self.view addSubview:[[UILabel alloc] initWithFrame:CGRectMake(40, 80, 200, 100)]];
}
This code won't display the label with the red background.
If, on the other hand use this code:
- (void)viewDidLoad {
[super viewDidLoad];
[[UIImageView appearance] setBackgroundColor:[UIColor redColor]];
[self.view addSubview:[[UIImageView alloc] initWithFrame:CGRectMake(40, 80, 200, 100)]];
}
then the red area is visible.
Even if I change the property, i.e. change the font instead, or the text color (and display some text of course), no changes are visible.
On most S.O. questions regarding UILabel and appearance, although some people did have some problems, in general it seems to work, so I don't understand what I am doing wrong here.
The comments from #matt made me find the error. (Unfortunately he has retracted his answer so I can't give him the credits he deserves, if #matt you are listening, please give another answer in order to get the credits).
The reason why it was that, it is because it seems to be a Bug in iOS 12.
Under (beta) iOS 13 the behavior is as expected.
Related
I am setting up an iOS app and have implemented a Facebook like button. Problem is, when I try to adjust the size according to their documentation found here, it just does not resize. Here's my setup code which is all the code that uses the like button:
(ViewController.h)
#property FBSDKLikeControl* likeButton;
(ViewController.m)
#synthesize likeButton;
- (void)viewDidLoad {
...
likeButton = [[FBSDKLikeControl alloc] initWithFrame:CGRectMake(20, 20, 128, 64)];
likeButton.objectID = #"https://www.facebook.com/mypage";
}
- (void)showLikeButton {
[self.view addSubview:likeButton];
}
- (void)hideLikeButton {
[likeButton removeFromSuperview];
}
When showLikeButton is called (which is after the app has been running a little while), the button shows up as expected, but no matter what size I initialize the button with, it doesn't change its size. Any ideas? Thanks in advance.
Use FBSDKLikeButton in place of FBSDKLikeControl. It inherits from UIButton and responds to sizing changes, although the image resolution suffers.
This problem can be demonstrated by creating a new project in Xcode (I am using version 6.4) and using the following code:
#interface ViewController ()
#property (nonatomic, strong) UITextField * myTextField;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, self.view.frame.size.width-100, 50)];
self.myTextField.textAlignment = NSTextAlignmentRight;
[self.myTextField becomeFirstResponder];
self.myTextField.backgroundColor = [UIColor lightGrayColor]
self.myTextField.adjustsFontSizeToFitWidth = YES;
self.myTextField.font = [UIFont systemFontOfSize:40];
[self.view addSubview:self.myTextField];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.myTextField.text = #"1.5241578750119E18";
}
When running this project in iOS Simulator (iPhone 5 or 5S), the cursor is initially displayed after the last "1", and the "8" is not visible until a new character is typed.
This appears to be a bug by Apple, but my question is: is there a workaround for now that will force the text to right-align and show the cursor in the correct position?
To clarify the question further, the issue occurs when the text is set programmatically. I expect to see this:
But instead I am seeing this (note that the entire number is not visible and the cursor is showing after the "1" instead of the last digit which is an "8"):
This is a bug, existing in iOS, since iOS 7. Issue can be reproduced in stock applications like Settings as well. It affects text fields only when NSTextAlignmentRight is used. The original bug ID logged into Radar for this issue is 14485694. You may use centre or left text alignments, to circumvent this problem.
I would also suggest to file a new bug report to Apple,
Try this
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
self.myTextField.leftView = paddingView;
self.myTextField.leftViewMode = UITextFieldViewModeAlways;
it will add some space to the left side of your TextField so that your cursor starts at correct position.
Hope it helps.
If I change your code to this:
- (void)viewDidLoad {
[super viewDidLoad];
self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(5, 50, self.view.frame.size.width-10, 50)];
self.myTextField.textAlignment = NSTextAlignmentRight;
[self.myTextField becomeFirstResponder];
self.myTextField.backgroundColor = [UIColor lightGrayColor];
self.myTextField.adjustsFontSizeToFitWidth = YES;
self.myTextField.font = [UIFont systemFontOfSize:60];
[self.view addSubview:self.myTextField];
}
And start typing in those numbers, the cursor ends up along the right edge of the text field just as it's supposed to.
Even when I start the app with the default value in the text, I see the cursor along the right edge of the text field fine.
Listening to UITextFieldTextDidChangeNotification notification instead of UIControlEventEditingChanged will fix the issue.
I met the same issue, try add dummy code like this
(void)textFieldDidBeginEditing:(UITextField *)textField {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
textField.text = textField.text;
});
}
Image 1
Image 2
Image 3
1st image is the settings screen and everything behaves normally
upon the first loading of this screen.
2nd image is the policy
screen which is pushed from the settings screen.
3rd image upon returning from the policy screen back to the settings screen shows the UI Slider now doesn't show the track image. The behavior still works normally, but the track is just gone. It's a very mysterious bug to me. If I go to another screen in the app and then hit settings it works fine again, but when I come from the policy screen the track image is gone.
I am using the default UISlider. I am using xib files for the different screens.
When the Policy Button is clicked...
EditorialPolicyViewController *policyView;
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
policyView = [[EditorialPolicyViewController alloc]initWithNibName:#"EditorialPolicyViewController_iPad" bundle:nil];
}
else
{
policyView = [[EditorialPolicyViewController alloc]initWithNibName:#"EditorialPolicyViewController_iPhone" bundle:nil];
}
[self.navigationController pushViewController:policyView animated:YES];
ViewDidLoad - setting up the uislider
NSString *textSize = [[NSUserDefaults standardUserDefaults] valueForKey:#"Text Size"];
float textSizeFloat = [textSize floatValue];
self.textSizeSlider.value = textSizeFloat;
[self.textSizeSlider setMaximumValue:16.0];
[self.textSizeSlider setMinimumValue:11.0];
Let me know if you have any other questions. I am out of ideas on how to solve this bug.
UPDATE
Found that the code below was "theming" the slider but when I removed it, it worked fine. What is a way i can keep the theme color and not get it to disappear?
[[UISlider appearance] setThumbTintColor:[UIColor colorWithRed:0/255.0f green:75/255.0f blue:152/255.0f alpha:1.0f]];
[[UISlider appearance] setMinimumTrackTintColor:[UIColor colorWithRed:164/255.0f green:75.0f blue:25/255.0f alpha:1.0f]];
[[UISlider appearance] setMaximumTrackTintColor:[UIColor colorWithRed:204/255.0f green:204/255.0f blue:204/255.0f alpha:1.0f]];
This is the well known bug in SetMaximumTrackTintColor.
Remove this line:
[[UISlider appearance] setMaximumTrackTintColor:[UIColor colorWithRed:204/255.0f green:204/255.0f blue:204/255.0f alpha:1.0f]];
and your app will work fine.
It is correct that this is a known bug. But have found a way to work around it so that you can still keep your theming:
[[UISlider appearance] setMaximumTrackTintColor:[UIColor colorWithRed:204/255.0f green:204/255.0f blue:204/255.0f alpha:1.0f]];
In UIViewController's where I use a slider, I set the maximumTrackTintColor in the viewDidLoad: method
- (void)viewDidLoad
{
[super viewDidLoad];
_slider.maximumTrackTintColor = [UISlider appearance].maximumTrackTintColor;
}
This works for me, so I wanted to share!
you can move the appearance changes into the viewDidLoad: function.
this way the appearance changes will only happened once.
- (void)viewDidLoad
{
// Appearance changes here
}
it works for me
Here is a picture describing the problem. Notice that the area above the UISearchBar does not have the background as below it. I don't want the gray color, I want the dark maroon background when pulling down on the UITableView.
Everything I know of is set to [UIColor clearColor], including backgrounds of views, etc. It works fine in iOS 6 but not iOS 7!
I've also attached a sample project so someone can take a look and see what I am doing wrong here?
Click here to download my sample project
Maybe I am just missing something stupid?
Thanks!
Two things you could do: 1) add an explicit clear background view to the UITableView. Or, 2) set your UIImageView to be the background view of the UITableView.
Either of these work. Here's the code that makes your sample work:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
backgroundImageView.image = [UIImage imageNamed:#"Film_bg.png"];
[self.view addSubview:backgroundImageView];
[self.view sendSubviewToBack:backgroundImageView];
// solution 1
// self.tableView.backgroundView = [UIView new];
// self.tableView.backgroundView.backgroundColor = [UIColor clearColor];
// solution 2
self.tableView.backgroundView = backgroundImageView;
}
I have a UIPickerView which works correctly, now I want to add a button above it so that I can dismiss it.
and here is my code where I initiate a UIPickerView as well as its dismiss button:
- (UIPickerView *)creatPickerView {
UIPickerView *tempPickerView = [[[UIPickerView alloc]
initWithFrame:CGRectMake(kPickerViewX, kPickerViewY, kPickerViewWidth, kPickerViewHeight)] autorelease];
tempPickerView.showsSelectionIndicator = YES;
tempPickerView.delegate = self;
tempPickerView.dataSource = self;
UIButton *pickerButton = [[UIButton alloc] initWithFrame:CGRectMake(270, -32, 50, 32)];
[pickerButton setBackgroundImage:[UIImage imageNamed:#"hidePicker.png"]
forState:UIControlStateNormal];
[pickerButton addTarget:self action:#selector(hidePicker)
forControlEvents:UIControlEventTouchUpInside];
[tempPickerView addSubview:pickerButton];
[pickerButton release];
[self.view addSubview:tempPickerView];
return tempPickerView;
}
and it works well on my iPhone 4.3 Simulator, like this:
apparently there is a button on the upper right of the pickerView,
problem is, when I run the app in my device - a 5.0.1 iPhone4 and a 4.2.1 iTouch, the button is missed like it has never been added to the pickerView.
Can anyone help me with this?
Thanks a lot and a lot!
I found the reason, it seems the png has some problem,
after I change another png, it comes up in the screen!
but the real problem is that I place the button outside of the pickerView which results in the button's untouchableness.
But anyway the pickure is only a small problem.