I need a UITableView with no background color at all since I have the same Background image for
every page within my app.
I know this was asked several times before, but none of the solutions posted here work for me.
I tried the
.backgroundView = nil
.backgroundColor = [UIColor clearColor]
thing for cells and tableview but the result was that I got a white background.
Some posts said that this is the new iOS7 standard.
I tried to set a color with an alpha value like this
.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
but that just gives me a black cell instead of a transparent one. So Aparently everything except the alpha value is applied here.
Also I got the problem that if you scroll the table over the "borders" there is still a white background. Is this the normal background of the tableview itself and would not be seen once the tableview did finally has an alpha value?
Oh, ofc appling an opacity-value from 0 in the inspector won't work either.
Am I missing something, is there a new way of doing this, is it even possible to have UITableViews that are complete transparent (excluding labels and images I've put in there of course)?
If that matters, I am currently using xCode 5.1.1
Thanks for any help.
Try:
cell.contentView.backgroundColor = [UIColor clearColor];
Related
I'm presenting a view in an NSPopover, using code based on this sample code.
The view, and all of its sub-views, are layer-backed. There's a single NSImageView, and several non-editable NSTextFields. The text fields backgroundColors are set to [NSColor textBackgroundColor], and their textColors to [NSColor textColor]. In this way, the text is black if one is using the normal theme, and white if one is using the "dark menu bar and Dock" option (which I'll refer to as "dark theme" from now on). This all works fine, and it looks a little somethin' like this:
Light theme:
Dark theme:
The problem comes when I animate the NSImageView up off the view. As it intersects with the NSTextFields, the image appears to blend with the text fields in an unappealing manner. It happens in both light and dark themes, but it's more icky-looking (it's a technical term) in the dark theme. Dig it:
The code to animate it looks basically like this:
CABasicAnimation* positionAnimation = [CABasicAnimation animationWithKeyPath:#"position"];
positionAnimation.fromValue = [NSValue valueWithPoint:fromPoint];
positionAnimation.toValue = [NSValue valueWithPoint:toPoint];
positionAnimation.duration = imageAnimationDuration;
[self.imageView.layer addAnimation:positionAnimation forKey:#"position"];
self.imageView.layer.position = toPoint;
What have I tried? Oh, what haven't I tried?
First off, my own views don't have any kind of NSVisualEffectView going on. But it seems that NSPopover adds that on its own; you can clearly see my desktop bleeding through the popover in the animation above. That's fine; it's actually a nice effect. But, thinking that my NSImageView was trying to be vibrant, I subclassed NSImageView just to return NO from allowsVibrancy. No change in behavior.
Next, I subclassed NSView to return NO from allowsVibrancy, and made the parent view of my view an instance of that. No change in behavior.
My NSTextFields are set with drawsBackground = NO, so I changed them to YES. No change in behavior. Then, leaving drawsBackground = YES, I set both text field's backgroundColors to [NSColor clearColor]. Here's where it gets weird. This does make the weird drawing go away, but it changes the text color of one of the text fields (the smaller one) to black. Wut? See below.
I gave up on the background colors, and started messing with the text colors. I found that if I set the textColor of the text fields to a discrete color (say, [NSColor blackColor] or [NSColor whiteColor], then the weird drawing problem also goes away. It seems only to get weird when using colors which adapt with the theme such as [NSColor textColor]. That's super lame, because the whole point of using something like [NSColor textColor] is that it adapts to the theme. I could probably hack around and figure out what theme is active and set the colors manually, but I really don't want to go that route if I can help it.
I promise there's a question in here somewhere, and, mercifully, here it is:
How can I fix the animation issue shown above, while still using colors which properly adapt to the current theme?
Sample project on GitHub.
Edit:
The desired result is to have no blending between the image and the text. Something like this:
The image I used in the sample app here maybe isn't the best example to convey the sheer yuckiness of the animation I'm seeing in my actual app. The image in the sample is already mostly white, while in my actual app it's mostly black, and it truly looks horrible when blended with white text.
Visual Effect Views (like used in NSPopover) are totally messed up in OS X Yosemite and they are causing your problem here. It is not your fault, these views are totally buggy.
A workaround on Yosemite should be to set the appearance property of each NSTextField to NSAppearanceNameAqua. Because if the labels don't try to do some weird vibrancy effect, they can't mess things up. The labels still look the same and the strange effect is gone.
My words in code:
self.titleLabel.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
self.descriptionLabel.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
Luckily NSVisualEffectView's are working fine now in El Capitan. So your code should work fine there..
When setting the tint color of a UISegmentedControl using the Appearance API, the color of the text in each unselected segment takes the color of the UILabel instead only after switching tabs.
A sample program to test this (screenshots below):
Load the program and look at the first tab. Everything is normal, the labels are red and the segments are blue.
Switch away to the second tab, everything is still fine.
Switch back to the first tab, you will see that the segments have changed to be red instead of blue like they should.
App was just loaded, everything is fine:
After switching tabs, color is wrong:
Code responsible (in the app delegate for testing, but happens elsewhere):
[[UILabel appearance] setTextColor:[UIColor redColor]];
[[UISegmentedControl appearance] setTintColor:[UIColor blueColor]];
I have sent this information to Apple in a bug report. They asked for a sample project, but I haven't gotten an answer yet. This only shows up on IOS 7.1. On 7.0, this doesn't happen.
Are there any suggestions or temporary fixes that would resolve this? It makes my app look bad even though I don't think it's my fault (the red is just to test, that would make anybody's app look bad). I have tried setting controls manually by setting the tint of the specific control instead of using the appearance API, but the problem is still there.
As discussed in the comments, use [[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setTextColor:[UIColor blueColor]]; to set the appearance of the internal label contained in a segment control.
Per the Apple documentation: Setting the tintColor property by using the appearance proxy APIs is not supported in iOS 7.
iOS 7 UI Transition Guide
You can also specify text attributes like of UISegmentedControl like font, using setTitleTextAttributes:forState.
I have a UIDatePicker in my view and have set the background color of the UIDatePicker:
self.datePicker.backgroundColor = [UIColor lightTextColor];
self.datePicker.layer.cornerRadius = 10;
This successfully puts a background behind the UIDatePicker (which in iOS7 is essentially transparent) but fails to make the rounded corners for the background that I am looking for (I do this same thing for an image on the screen and it works perfectly).
It seems that the corner radius doesn't affect the background color.
Is there a way to fix this problem by setting a corner radius for the background color (or any another solution).
The reason I want to do this is because the ordinary UIDatePicker looks awkward in the view I have constructed and looks much better with a background color.
However, all the other items in the view have rounded corners and I want the UIDatePicker to match them.
Thanks.
You have to add layer.masksToBounds=YES;
Try this,
self.datePicker.backgroundColor = [UIColor lightTextColor];
self.datePicker.layer.cornerRadius = 10;
self.datePicker.layer.masksToBounds=YES;
UIBuilder Swift 5 solution:
See picture, it's the same as #ToseefKhiji's solution but even easier to execute for fans of UIBuilder controls.
This is how it comes out (I have added other features to the picker programmatically, such as borderWidth, color, etc...)
UIBuilder is underappreciated by Swift/iOS coders as everyone has rushed to SwiftUI as the new new thing.
Last week I asked a question about a Simulator bug with NSAttributedString not displaying: iOS 7 Simulator Bug - NSAttributedString does not appear
Unfortunately it now appears this is not a simulator bug but an iOS 7 bug. I have now reproduced this issue on an iPhone 5 device.
The bug appears to be the combination of using NSUnderlineStyleAttributeName & NSParagraphStyleAttributeName as attributes for a NSAttributedString.
I have only tested on two iOS 7 devices so far, and the issue has only appeared on one of them. Even after they have both been upgraded to the exact same version:
1st iPhone 5 with iOS 7.0 (11A465): Text does NOT appear
1st iPhone 5 after upgrading to 7.0.2 (11A501): Text does NOT appear
2nd iPhone 5 running iOS 7.0 (11A4449d): Text displays correctly
2nd iPhone 5 after upgrading to 7.0.2 (11A501): Text does NOT appear
So it appears Apple introduced this bug after iOS 7.0 (11A4449d). I've filed a bug with them and will update you on any response I get.
Steps to reproduce bug
If you are running iOS 7.0.2 then you should be able to reproduce this bug.
Either download and run this project on your device https://github.com/rohinnz/iOS-7-BUG---NSAttributedString-does-not-appear
or
1) In Xcode 5 create a new 'Single View Application'. Call it whatever.
2) In ViewController.m, replace the viewDidLoad method with:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableParagraphStyle* paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentCenter;
NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:#"Lorem ipsum dolor sit" attributes:
#{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle),
NSParagraphStyleAttributeName:paragraph}];
UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 0, 0)];
myLabel.backgroundColor = [UIColor greenColor];
myLabel.attributedText = attrStr;
[myLabel sizeToFit];
[self.view addSubview:myLabel];
}
3) Compile and run on your device. Depending on your version of iOS 7, the text will either display, or will not. The UILabel's background color will display in both cases.
Screenshots
iPhone 5 with iOS 7.0 (11A465)
iPhone 5 with iOS 7.0 (11A4449d)
My Question
Is anyone able to reproduce this issue on a device?
I have found a workaround for this bug. In your github code, to use the workaround, you would say this:
NSAttributedString* attrStr =
[[NSAttributedString alloc] initWithString:#"Lorem ipsum dolor sit\n" // <---
attributes:
#{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle),
NSParagraphStyleAttributeName:paragraph}];
UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 0, 0)];
myLabel.backgroundColor = [UIColor greenColor];
myLabel.attributedText = attrStr;
[myLabel sizeToFit];
myLabel.numberOfLines = 2; // <---
I have made two changes: I've appended a newline character to your original string, and I've set the label's numberOfLines to 2.
What the workaround does is to force the label's text up against the top of the label. This seems to solve the problem. In other words, the bug appears to stem from the label's attempt to vertically center its text; if you deliberately make the text too long for the size of the label by juggling the label's height, numberOfLines, and excess newline characters at the end of the text, the attributed string will be drawn.
EDIT I've just found another workaround along the same lines: let the label resize itself to fit the text. See my code and explanation here: https://stackoverflow.com/a/19545193/341994 In that code, I do the same thing from the opposite end, as it were: I give the label a fixed width constraint but a flexible height constraint, and by setting its own height, the label brings the top of its text up against the top of itself, and thus is able to display the text correctly. In other words, this is just another way of preventing the label from centering its text vertically, which is what seems to trigger the bug.
FURTHER EDIT I have the sense that this bug may get fixed in iOS 7.1.
I also had the same problem when setting the background color on text of a UILabel in a UITableViewCell. My workaround was to use a UITextView with UserInteraction disabled instead of a UILabel in the cell and it worked.
Update: Found the issue only appearing with UILabel included in Basic UITableViewCell.
Update 2: Also found that the problem does not occur when a UILabel wraps to multiple lines of text. One workaround is to force text to wrap by adding a newline and space. Very hacky, but it works. Make sure numberOfLines is set to zero and lineBreakMode is set to NSLineBreakByWordWrapping.
I had the same issue in my application. It was occurring in the simulators, as well as on my device (iPhone 5 running 7.0.2 (11A501)). I then realized that my UILabels living in other ViewControllers were displaying NSAttributedStrings using the NSUnderlineStyleSingle attribute properly.
After some troubleshooting. It seems that if you're using the default font (I'm using System 17.0) and your UILabel has a height of less than 62 pixels, it will display correctly regardless of what background color, text color, or alignment you are using. A change of the UILabel's height to a value greater than 61 pixels, allowing auto-sizing to change the height for you, or a change of the font to a Custom one will result in the disappearing of the underlined NSAttributedText.
At first I thought this issue might be due to my positioning the UILabel behind the new Status Bar (or lack thereof), but even in positions which would interact with this new feature, the height-rule still held. I find it hard to believe that the height of the UILabel would cause such an issue, but that's the conclusion I came to.
Wow, took me a while to find this. Looks like I'm have a similar problem as Indi. Setting the background color of an attributed string caused the text to just disappear. Only place I can reproduce this is on a device running iOS 7.0.3.
workaround: use an image view
CGRect rect = self.frame;
CGRect rr = [attribText boundingRectWithSize:rect.size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesDeviceMetrics context:nil];
UIGraphicsBeginImageContextWithOptions(rr.size, NO, 0.);
[attribText drawWithRect:rr options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesDeviceMetrics context:nil];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imView = [[UIImageView alloc]initWithImage:image];
[self addSubview:imView];
I noticed this problem occurring in a similar way but ended up with a different solution. The string would sometimes disappear, while the solution proposed above helped ensure the text did not disappear, the text would often show up without any of the attributes I had set (strikethrough, different colours, etc.)
Here's the setup:
A legacy project using springs and struts being built using Xcode 6.1.1 and iOS SDK 8.1. The problem was more noticeable on iPad devices as compared to iPhone devices (~ 5% of the time on iPhones vs. 95% on iPads). Regardless whether I used numberOfLines, sizeToFit or other methods, the attributes would not show up correctly on an iPad or iPhone 100% of the time.
The solution was to switch to Auto Layout and employ the solution above (numberOfLines = 2, sizeToFit seemed optional for my situation)
It seems there's a bug with Attributed Text on UILabels with Springs and Struts when they get stretched horizontally.
Hope this helps someone!
i had this problem also and it seams to manifest only on specific languages and on iOS 7.0 , i had this issue when i want to underline the text in chinese, solved the problem with [ label sizeToFit] hope it will help ;)
Constantin.
So what I've been trying to do was to put a NSTableView on a black background. However, for some kind of reason there's this white outline that wraps the NSTableView like this:
What I've got here is a translucent window with a subclassed NSTableView so that I can customize it's color. Alternating rows are turned on.
I've tried setting the grid color and background to a color with 0 alpha value, however nothing changes. Does anyone know why or how I can fix this? Thanks!
I think in Your NSTableView horizontal grid is turned on. You can turn off it in Attributes inspector and change colors also (grid and background).
Have you tried this:
tableView.backgroundColor = [UIColor clearColor];
tableView.separatorColor = [UIColor grayColor];
Your screenshot link is broken, so it’s hard to tell what you’re looking for exactly.
This answer might interest you, though: https://stackoverflow.com/a/4349459/135712
[yourTableView setIntercellSpacing:NSMakeSize(0.0, 0.0)];