App layout messed up after updating to newest iOS - ios

Background: My company has support contracts for an app to always make it work for the two latest iOS versions. Now instead of updating the deployment target and building it with the newest iOS version they just checked if the app that was built with iOS7 and a deployment target of iOS5(!!!) still worked on iPads with the newest iOS. And with some adjustments they somehow managed to make it work.
Now it's all coming back: The app won't work for iOS11, it didn't even build for arm64 till now. And I got the awesome task of making it work.
Making the app build with arm64 wasn't even that hard, just had to replace some libraries. But when building for iOS11 the layout of the app is messed up. One small example: On the login screen we got some buttons that will just cut their labels for no apparent reason. There is enough space for the text but it's still cutting off. Everything else on this view is the same as before.
The code for the buttons (Interface Builder hasn't been used in this app) looks completely fine to me, it's pretty much the same how I would do it today. So my guess is that there are some build settings or similar stuff I have to change for it to look like it should. Can anyone give me a hint here on what to do?
If you want to check out the code for the button, here you go. I can't give you all the code I have since it's company code but this should be no problem:
loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
loginButton.titleLabel.font = BUTTON_FONT_SMALL;
loginButton.frame = CGRectMake(x, passwordField.frame.origin.y + passwordField.frame.size.height + spacerHeight * 2, fieldLength, labelHeight);
loginButton.backgroundColor = [UIColor clearColor];
loginButton.titleLabel.textColor = [UIColor blackColor];
[loginButton setTitleColor:[UIColor colorFromString:#"47;72;127"] forState:UIControlStateNormal];
[loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[loginButton setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
[loginButton setBackgroundImage:buttonImageActive forState:UIControlStateSelected];
[loginButton setBackgroundImage:buttonImageActive forState:UIControlStateHighlighted];
loginButton.tag = 1;
[loginButton addTarget:self action:#selector(login_Tapped:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:loginButton];

Related

Question mark when tapping or holding font icons

So I recently started some tinkering with my app to get it iOS 11 compatible. Thankfully most of it seems to be.
However I did notice, that in my toolbar if I tap or tap and hold an icon, which is supplied by a ttf file from fontello, I get a question mark box.
Example of icon:
menu = [[UIBarButtonItem alloc] initWithTitle:#"\ue811" style:UIBarButtonItemStylePlain target:self action:#selector(openMenu:)];
[menu setTitleTextAttributes:#{NSFontAttributeName:
[UIFont fontWithName:#"fontello"
size:23],
NSForegroundColorAttributeName:[[UIColor alloc] initWithWhite:1.f alpha:1.f]}
forState:UIControlStateNormal];
It works fine in the 10.3.1 simulator. Just iOS 11 seems to be goofed up. I've read about the fixes for devices, which means to update the OS, but the simulator is running 11.2, so in theory it should be fixed.
Is anybody else having this issue? Know of a fix?
Just add title text attributes for UIControlStateSelected also:
[menu setTitleTextAttributes:#{NSFontAttributeName:
[UIFont fontWithName:#"fontello"
size:23],
NSForegroundColorAttributeName:[UIColor greenColor]}
forState:UIControlStateSelected];
As mentioned in the comment, iOS 11 requires you to have a setting for normal state and selected/highlighted state. Below is what is working for me. Not ideal to have extra code depending on many buttons you have, but oh well.
[menu setTitleTextAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"fontello"size:23],
NSForegroundColorAttributeName:[[UIColor alloc] initWithWhite:0.f alpha:1.f]}
forState:UIControlStateNormal];
[menu setTitleTextAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"fontello"size:23],
NSForegroundColorAttributeName:[[UIColor alloc] initWithWhite:0.f alpha:0.5f]}
forState:UIControlStateHighlighted];

iOS 9 View Not appearing

I have noticed a that a uibutton is not showing in my app when run in iOS 9 (works fine in earlier OS versions). I know several have noticed a similar issue with storyboards (https://forums.developer.apple.com/thread/14003), though I am not using those and am instead building my view programmatically. There is no autolayout or autoresizingmask to worry about.
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(SUG_MARGIN, self.mainView.bounds.size.height - 40 - margin, self.mainView.bounds.size.width-margin*2, 40);
[button setTitle:NSLocalizedString(#"...", nil) forState:UIControlStateNormal];
button.titleLabel.font = //...;
button.backgroundColor = //...;
[self.mainView addSubview:button];
NSLog(#"main:%#\nadd:%#\nsuper:%#", NSStringFromCGRect(self.mainView.frame), NSStringFromCGRect(button.frame), button.superview);
That nslog shows everything set up correctly.
I have similar buttons all over the app, but this is the only one that is not appearing, which is why I am scratching my head.
Refactored code to assign only measure frames in viewWillLayoutSubviews and it now works. strange.

why images in the Images.xcassets are not displayed?

There are some image resources in my Images.xcassets. Recently they cannot be displayed in the app when testing in the iPhone/iPad devices.
I guess they are not copied to the Bundle when build and package. Then i check the
Copy Bundle Resources in Build Phases. The Images.xcassets is in here.
One weird situation is it works well in simulators, but failed in the real devices. For a clean test, every time i will delete the app and then run again.
Some of snippets are as below:
UIButton *button_Share = [[UIButton alloc]initWithFrame:CGRectMake(13,33,64,20)];
[button_Share setBackgroundColor:[UIColor clearColor]];
[button_Share setUserInteractionEnabled:YES];
[button_Share setImage:[UIImage imageNamed:#"share_btnM#2x.png"] forState:UIControlStateNormal];
[button_Share addTarget:self action:#selector(click_Share) forControlEvents:UIControlEventTouchUpInside];
[imageview_ButtonBg addSubview:button_Share];
UIButton *button_AddTo = [[UIButton alloc]initWithFrame:CGRectMake(112,33,97,20)];
[button_AddTo setBackgroundColor:[UIColor clearColor]];
[button_AddTo setUserInteractionEnabled:YES];
[button_AddTo setImage:[UIImage imageNamed:#"add_cart_btnM#2x.png"] forState:UIControlStateNormal];
[button_AddTo addTarget:self action:#selector(click_AddToCart) forControlEvents:UIControlEventTouchUpInside];
[imageview_ButtonBg addSubview:button_AddTo];
UIButton *button_Buy = [[UIButton alloc]initWithFrame:CGRectMake(243,33,64,20)];
[button_Buy setBackgroundColor:[UIColor clearColor]];
[button_Buy setUserInteractionEnabled:YES];
[button_Buy setImage:[UIImage imageNamed:#"buynow_btnM#2x.png"] forState:UIControlStateNormal];
[button_Buy addTarget:self action:#selector(click_Buy) forControlEvents:UIControlEventTouchUpInside];
[imageview_ButtonBg addSubview:button_Buy];
images in the image set are as below:
Any help would be appreciated.
The problem is that you have not understood the principle of asset catalogs. They give to their assets the names of the image sets. You no longer use the old size-based image names. That is the whole point of asset catalogs! You use one name and the right asset is chosen for you, appropriate to your hardware.
Thus, for example, this call is wrong:
[UIImage imageNamed:#"share_btnM#2x.png"]
Your image set is called share_btnM, so you would say simply:
[UIImage imageNamed:#"share_btnM"]
You need to go through all your code, find all your imageNamed: calls, and fix that everywhere. Then your images will work.

UIButton title text is not updated even if I update it in Main thread

I'm trying to change the title of an UIButton I've created programmatically, when the user clicks in it. So, this is my code to create the UIButton:
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)];
[myButton setBackgroundColor:[UIColor blackColor]];
[myButton setAlpha:0.7];
[myButton setTitle:#"Hello" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(userClicked:) forControlEvents:UIControlEventTouchUpInside];
[parentView addSubview:myButton];
And, in my userClicked: method, I do:
-(void) userClicked:(UIButton*)button
{
NSLog(#"USER CLICKED!!!");
if ([NSThread isMainThread])
{
NSLog(#"is main thread");
}
[button setTitle:#"Bye" forState:UIControlStateHighlighted];
[button setTitle:#"Bye" forState:UIControlStateNormal];
[button setTitle:#"Bye" forState:UIControlStateSelected];
[self someLengthyComputation];
}
The weird thing is that I can see the log messages printed:
USER CLICKED!!!
isMainThread
But, the title of the button does not change! What am I doing wrong?
EDIT: Setting the title for several states doesn't work either.
EDIT2: If I print the description of button in the debugger window of Xcode, it shows the right title!
Printing description of button->_titleView:
<UIButtonLabel: 0xa4c9310; frame = (95 216; 130 22); text = 'Bye'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xa44f080>>
This worked for me to update the title text (iOS 7.1, Xcode 5.1):
button.enabled = FALSE;
[button setTitle:#"Test" forState:UIControlStateNormal];
button.enabled = TRUE;
I was having the same problem, and I noticed that everywhere else I was setting the attributedTitle. So any updates to the title were not affecting the attributed title.
My solution:
[button setAttributedTitle:#"" forState:UIControlStateNormal];
instead of
[button setTitle:#"" forState:UIControlStateNormal];
At the moment, the shortest work around I came with is calling:
[button setNeedsLayout];
After updating the title.
This seems to happen in iOS 7.1.
All my buttons, which were behaving correctly in previous iOS versions (or maybe just compiled with previous SDKs) suddenly stopped doing that when compiled in Xcode 5.1.1 SDK 7.1.
Looks like Apple's bug.
I had a similar problem using storyboards. Using the answers above I had to use
[mybutton setTitle:#"SomeText" forState:UIControlStateNormal];
[button setNeedsLayout];
[button layoutIfNeeded];
AND I had to make sure that the button type was 'Custom' not 'System' in the attributes inspector.
Please see if this might help you...when the button is clicked check for condition if buttonToggled...like below when you have a function like changeButtonText
-(IBAction)changeButtonText:(id)sender {
if (!buttonToggled) {
[sender setTitle:#"Initial Text" forState:UIControlStateNormal];
buttonToggled = YES;
} else {
[sender setTitle:#"Alternative Text" forState:UIControlStateNormal];
buttonToggled = NO;
}
}
There are several issues in your code:
You are assigning callback to the button:
#selector(userClicked:)
but your code is in another method:
-(void)userTapOnTapToRefreshView:(UIButton*)button
To fix that you need to implement something like this:
-(void)userClicked:(id)sender
{
[(UIButton*)sender setTitle:#"Bye" forState:UIControlStateNormal];
}
Also this part of code does not make sense for me:
[parentView myButton];
Try to change it to:
[parentView addSubview:myButton];
Hope it will help :)
For Swift 3 to 5 just use the following:
button.setTitle("My title", for: .normal)
or for a attributed text use this:
button.setAttributedTitle(<AttributedString>, for: .normal)
This is kinda late and somewhat relates to Walter Schurter's response:
I had a similar issue where my button text was being set correctly until I updated to 7.1. Then I found that since my button was disabled, I had to set the title color and title text for the disabled state to get the text to show up. Then everything worked like a charm!
In iOS 7, UIButton's title is not updated when it is disabled. It seems like a bug on iOS 7.
As a workaround, update both normal and disabled title. It works!
[button setTitle:title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateDisabled];
Try this:
[button setTitle:#"Bye" forState:UIControlStateHighlighted];
or:
[button setTitle:#"Bye" forState:UIControlStateSelected];
You could also modify your void function:
-(void) userClicked
{
NSLog(#"USER CLICKED!!!");
if ([NSThread isMainThread])
{
NSLog(#"is main thread");
}
[myButton setTitle:#"Bye" forState:UIControlStateNormal];
}
In my case I tried everything but nothing was working.
Then I just changed button type from system to custom from storyboard. BOOM! everything started behaving normally.
Change the button type to 'Custom', instead of 'System' and it will work as expected :)
Well, is all about the enabled state of a UIButton, Apple has changed something in 7.1 that does not allow you to change the title if you have the UIButton on a disabled state, thats all.
Thanks Apple, i have lost 10 min. debuging an app that was working fine.
Just found out this morning, got updated XCode yesterday to the 5.1.1 and iOS to 7.1
It could be the button layout refresh issue.....
Try using...
[button setNeedsLayout];
[button layoutIfNeeded];
It will force button to update the layout.
Finally, I've figured it out. There were two problems:
1) button was not in state UIControlStateNormal.
2) I was calling a method performing a long computation just after setting the title, [self someLengthyComputation].
I've solved the problem by:
1) Setting the title for all states of the button.
2) Performing that big computation in another thread, not the main thread.
My working code now is:
-(void) userClicked:(UIButton*)button
{
NSLog(#"USER CLICKED!!!");
if ([NSThread isMainThread])
{
NSLog(#"is main thread");
}
[button setTitle:#"Bye" forState:UIControlStateHighlighted];
[button setTitle:#"Bye" forState:UIControlStateNormal];
[button setTitle:#"Bye" forState:UIControlStateSelected];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[self someLengthyComputation];
});
}
Thank you very much to everybody who has answered/commented!
This may be trivial, but if you set some button image (instead of background image) which fills the whole button frame, this will shift the title right and thus make it invisible.
If so, try changing button image to background image.
Per Apple developer guide, you should not set either the button title label text or color directly as a property (for example, do not do this: myButton.titleLabel.text=#"SomeText" or myButton.titleLabel.textColor=[UIColor blackColor]).
Rather, you set them using the UIButton setter functionality, like this:
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
...or like this:
[myButton setTitle:#"SomeText" forState:UIControlStateNormal];.
See the Apple guide.
Conclusion after trying many solutions is to use setAttributedTitle instead of setTitle.
To make the title string for AttributedString:
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:#" new Test "];
[button setAttributedTitle:attString forState:UIControlStateNormal];
By the way, this problem is not occasionally happen in normal but it suddenly happen mainly for the following reasons:
If you change the enabled state of a UIButton and try to change the title.
If you use an attributed value and then want to change it using setTitle, the attributed value is superior to the title in that case.
If you navigate to another view controller and then return back to update the button title.
There is no requirement to use:
[button setNeedsLayout];
[button layoutIfNeeded];
Instead, first set type as DEFAULT to CUSTOM
If you applied setAttributedTitle then use:
[button setAttributedTitle:[NSAttributedString new] forState:UIControlStateNormal];
Otherwise there is no need to change anything.
If the color of the text has not changed then apply same thing and set title color with:
[button setTitleColor:[any_color] forState:UIControlStateNormal];
None of the above worked for my case (I was having a button on top of a view presented with UIPopoverController), and I had to removeFromSuperview before setTitle, then addSubview back - hope this helps someone with similar issues
I doubt if 'button' passed in as a parameter is myButton.Anyway,if myButton is a member var,you can just [myButton setTitle:#"Bye" forState:UIControlStateNormal];
Try to declare you button as a property, either in the interface or in the implementation part of you view / view controller
#property(nonatomic,strong) UIButton* myButton;
then create it
self.myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)];
[_myButton setBackgroundColor:[UIColor blackColor]];
...
It should work. Don't ask me why really - I just think it's all about ARC / Modern memory management..
EDIT:
It should also work if you keep a reference on the button in your implementation..
#implementation myViewController
{
UIButton* myButton;
}
-(void)createButton
{
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)];
[myButton setBackgroundColor:[UIColor blackColor]];
...
}
...
instead of
yourButton.titleLabel?.text = "T"
use this
yourButton.setTitle("TITLE", for: .normal)
Always do addSubview before setting the text on UIButton.

Localizing buttons in Xcode

This is my first question so far so I would love some understanding!
I am localizing entire application. I have no problem with labels that while pseudolocation translate properly. However, trying to achieve the same with buttons it doesn't work.
The code is
[self.Clear setTitle:NSLocalizedString(#"Clear",nil) forState:UIControlStateNormal];
and the .string file in en.plist content looks like this
"Clear" = "Blah";
The pseudolocation doesn't work and I would really appreciate some help. The simulator keeps showing Clear.
If you have any questions regarding this, I will answer!
Please don't eat me :P
In Interface Builder, you can set 4 strings, one for each of the states in the "State Config" dropdown.
OR, alternatively, in code, you set the button title for each state:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:NSLocalizedString(#"21.title", #"Norm!") forState:UIControlStateNormal];
[button setTitle:NSLocalizedString(#"21.title-highlighted", #"hi btn") forState:UIControlStateHighlighted];
[button setTitle:NSLocalizedString(#"21.title-selected", #"sel btn") forState:UIControlStateSelected];
[button setTitle:NSLocalizedString(#"21.title-disabled", #"dis btn") forState:UIControlStateDisabled];
Courtsey : Localize IOS button label

Resources