I'd like to know what makes the backgrounds of subviews (labels for example) of a UITableViewCell become transparent while selected/highlighted. I need to avoid that behaviour for some subviews of my content view. I tried overriding the setSelected / setHighlighted methods with some success, but that transparency I wasn't able to reproduce. Any thoughts?
I think I got a somewhat close behaviour using the following piece of code
- (void) setView:(UIView*) view asHighlighted:(BOOL) highlighted {
if([view isKindOfClass:[UILabel class]]){
UILabel* label = (UILabel*) view;
[label setHighlighted:highlighted];
}
else if([view isKindOfClass:[UIImageView class]]){
UIImageView* imageView = (UIImageView*) view;
[imageView setHighlighted:highlighted];
}
if(highlighted){
[view.undoManager registerUndoWithTarget:view
selector:#selector(setBackgroundColor:)
object:view.backgroundColor];
view.backgroundColor = [UIColor clearColor];
}
else {
[view.undoManager undo];
}
for(UIView* subview in view.subviews){
[self setView:subview asHighlighted:highlighted];
}
}
Not sure this is the right way to use UndoManager but it works. You can use this in both setSelected and setHighlighted to mimic selection behaviour of UITableViewCells
Related
In my application i have a view
in that i have many UIElements, buttons, etc..
I need to set
UserInteractionEnabled:NO for all the ui elements in that view except one button.
I tried with
[self.view setUserInteractionEnabled:NO];
The Require button also the subview that self.view that button also apply same behavior.
I can able to apply individually but it is not a good way.
How should i set UserInteractionEnabled:NO for all the other ui elements except one button
for (UIView *view in [self.view subviews])
{
if (view.tag==101)
[ view setUserInteractionEnabled:YES];
else
[ view setUserInteractionEnabled:NO];
}
Check this out:
for (UIView *view in self.view.subviews) {
if (!([view class]==[UIButton class]) )
{
view.userInteractionEnabled = NO;
}
}
You can just add transparent subview in the front of your view and place button on this transparent view:
UIView* maskView = [[UIView alloc] initWithFrame:self.view.bounds];
maskView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:maskView];
[maskView addSubview:buttonView];
And be sure that this transparent view is the last added subview, or just push it to the front of view in viewWillAppear:
[self.view bringSubviewToFront:maskView];
Hi To remove SubViews from UINavigation bar i am using following code.I have label,button and imageVIew as subView in navigation bar.
for (UIView *view in self.navigationController.navigationBar.subviews) {
[view removeFromSuperview];
}
While i am running this it is removing the backGround image of the navigationBar which i added as
[self.navigationController.navigationBar setBackgroundImage:[UIImagem imageNamed:#"header-background"] forBarMetrics:UIBarMetricsDefault];
after removing subViews i am adding the background agin,But it is not adding.
Is there any way to remove only subViews of navigation bar without removing the background.
a fast option is to add a value to the tag property of the views you want to remove and check for it before removing the the subview, for example, assuming that you add a non-zero value to your subviews:
for (UIView *view in self.navigationController.navigationBar.subviews) {
if (view.tag != 0) {
[view removeFromSuperview];
}
}
Try this,
for (UIView *view in self.navigationController.navigationBar.subviews) {
if([view isKindOfClass:[UIImageView class]])
{
//change your bar image
}
else
{
[view removeFromSuperview];
}
}
How can I make a custom sublayer in a UITableViewCell disappear in setSelected:animated:?
Context:
I've added a custom background layer to my UITableViewCell by adding a CAGradientLayer inside of drawRect:, like so:
- (void)drawRect:(CGRect)rect{
[super drawRect:rect];
[[self providerLabel] setTextColor:kOUBlue];
[self addGrayLayer];
}
- (void)addGrayLayer{
[[[self contentView] layer] insertSublayer:[self grayGradientLayer] atIndex:0];
}
- (CAGradientLayer *) grayGradientLayer{
if (!_grayGradientLayer) {
CAGradientLayer *gradient = [CAGradientLayer layer];
UIColor *white = [UIColor colorWithWhite:1 alpha:2];
UIColor *gray = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
[gradient setColors:#[(id)white.CGColor, (id)gray.CGColor]];
[gradient setFrame:[self bounds]];
_grayGradientLayer = gradient;
}
return _grayGradientLayer;
}
When the user taps on a cell, the blue highlighting doesn't appear. So I've attempted to hide the CAGradientLayer in setSelected:animated: like so:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (selected) {
[[self grayGradientLayer] setHidden:YES];
}else{
[[self grayGradientLayer] setHidden:NO];
}
[[self contentView] setNeedsDisplay];
}
The blue background appears, but not until the push animation begins. Is there any way for me to make the default blue appear immediately? I've also tried to add a second colored CAGradientLayer and swap them manually, but it seems to me that the cell doesn't redraw immediately after I hide the layer in setSelected:.
I've tried to force the cell to redraw, using setNeedsDisplay with no luck. Any ideas?
After digging around, I found this question, I realized I was using the wrong method. It turns out I had to use the setHighlighted:animated: method.
My new code looks like this:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
[[self grayGradientLayer] setHidden:YES];
}else{
[[self grayGradientLayer] setHidden:NO];
}
}
I am trying to add a UISearchBar without the border around it, but it's simply not working.
This is what I have:
On the XIB file, I added UISearchBar. Style = Black Transparent. Tint = Default. None of the options (bookmarks, cancel button, scope bar, etc) are selected. Background = Default. Drawing = opaque (basically the default setting of the UISearchBar).
I have a property for UISearchBar - searchBar.
In the implementation file, when the view loads, I have:
[[self.searchBar.subviews objectAtIndex:0] removeFromSuperview];
But for whatever reason, the background (the dark border around the box) is simply not going.
I tried [[self.searchBar.subviews objectAtIndex:0] setAlpha:0], but that's not working at all either.
Any help please?
This worked for me
for (UIView * view in [[[self.searchBar subviews] objectAtIndex:0] subviews])
{
if (![view isKindOfClass:[UITextField class]])
{
view .alpha = 0;
}
}
Here is the code .....
for (id img in searchBar.subviews)
{
if ([img isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
[img removeFromSuperview];
}
}
happy coding :)
How about:
[self.searchBar setOpaque:NO];
[[_searchBar.subviews objectAtIndex:0] removeFromSuperview];
_searchBar.backgroundColor = [UIColor clearColor];
Hope this work for you, its working for me.
this work only in static UISearchBar, not if you want to show search CancelButton, this problem occur in devices having ios above 5.0.
For that you have to change/set BackgroundView of UiSearchBar.
Try This
for (UIView * view in [mySearchBar subviews]) {
if (![view isKindOfClass:[UITextField class]]) {
view .alpha = 0;
}
}
For those of you using 8.0 and above, the best solution is to simply use the UIAppearance protocol, by doing the following:
[[UISearchBar appearance] setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
You can also do it on one specific instance with:
[mySearchBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
if someone has same problem as I (see picture) code above could fix
[self.tableView setContentInset:UIEdgeInsetsMake(-1, 0, 0, 0)];
I have a normal Interace in interface builder (there's a UILabel and a UIButton). In code, I create a UIScrollView with a (unknown) number of UILabels (depends by the User). These labels are created in - (void)viewWillAppear to make sure the data is up to date. To remove the labels, in viewWillDisappear i'm calling
for(UIView *subview in [scrollView subviews])
{
[subview removeFromSuperview];
}
The problem is, after the view get's called again, the Objects created with interfacebuilder (the UILabel & UIButton) disappear. Do the get removed by calling [subview removeFromSuperview];? If yes, how to add them again? Everything which is created by code is still thereā¦
you should add tag to your labels that you add dynamically.
for(int i = 0; i < n; i++){
UILabel *label = [UILabel alloc...];
label.tag = 999999;
}
then.
for(UIView *subview in [scrollView subviews])
{
if([subview isKindOfClass:[UILabel class]] && subview.tag == 999999){
[subview removeFromSuperview];
}
}
Don't remove all subviews. Remove only the labels you added in viewWillAppear.
UILabel, UIButton and UITextfields are basically subclasses of UIView.
So if you remove all the UIViews under the scrollview, it will also remove them too.
for (UIView *myView in [scrollView subviews]) {
if([myView isKindOfClass:[UILabel class]]){
[myView removeFromSuperview];
}
if([myView isKindOfClass:[UIButton class]]){
[myView removeFromSuperview];
}
if([myView isKindOfClass:[UITextField class]]){
[myView removeFromSuperview];
}
}