How to force an element in a view to be non-transparent when parent view has alpha set to translucent - ios

I have a view that is slightly translucent, I set the alpha to about .75 and it contains a button as a sub element.
I want the button to be completely opaque and I set the opaque property in IB but still the button appears as translucent.
Any pointers?
Thanks!

A subview of a superview is allways minimum as transparent as its subview.
Subview alpha = 0.5 and superview alpha = 0.1 would result in an alpha of the subview of 0.05.
The only way of achieving that is changing the view hierarchy. Your subview must not be a subview any more. It may still be at the same position. (May require different position value).
An example.
Your background view is backgroundView
Your superview is firstView
Your subview is overlayView
Your current hierarchy is
backgroundView -> firstView -> overlayView
You should change that to:
backgroundView -> first View
\-> overlay View.
If your firstView's position is (10, 10) and your overlayView's position was (20,20) then change your overlayView's position to (30,30) because it now is within the coordinate system of background view.
For your code:
At some point you used to have
[backgroundView addSubView:firstView];
[fisrtView addSubView:overlayView];
change that to
[backgroundView addSubView:firstView];
[backgroundView addSubView:overlayView];
If you did not code that but defined it within IB then just move overlayView within the views tree accordingly so that it is on the same level as firstView.
Regardless whether you code it or draw it in IB, make sure that firstView comes first and overlayView comes next. Otherwise firstView could hide overlayView when its alpa is greater than 0.

Related

Make part of UIView visible

I want to add a "shadow" image on my view, but, i want part of my view to still be "visible". You better understand what i want to do, when look on screenshot:
I can add a UIView above my superview, but how could i make specific point "visible"? That actually mean make specific area of a view with different colour or opaque.
For your problem I had a way, try the below example.
1. First storyboard design: I had a UIButton
2. Added black View as a subview
UIView *blackView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
blackView.backgroundColor=[UIColor blackColor];
blackView.alpha=0.6f;
[self.view addSubview:blackView];
now result will be:
3. Add one more UIImageView with frame equal to button frame
UIImageView *imageView=[[UIImageView alloc]init];
imageView.frame=self.button.frame; //getting current UIButton bounds
imageView.image=[UIImage imageNamed:#"add_img.png"];
[self.view addSubview:imageView];
Now it looks same as what you want:
You have to add some views. A main view will be the container of your less-alpha view and of the opaque button.
MainView
|
------- UIView with 0.6 alpha
|
------- UIButton with 1 alpha
Indeed, if you change the alpha of the MainView, all subviews will be affected. Here, UIView with 0.6 will have the same frame as MainView but it will not affect UIButton alpha.
Make a new View, a parent View with clear color background. Add the black view and the button to the parent view and set the black view's alpha to 0.6 or whatever.
Try this:
Make a UIView called buttonBackgroundView, give it a black color and then
[self.view addsubView:buttonBackgroundView];
now make a UIView named plusView (or UIButton if this plus sign is a button) then
[self.view addsubView:plusView];
After that, give the alpha to the buttonBackgroundView
buttonBackgroundView.alpha = 0.6;
Happy coding!

UIScrollview position moves after touches begin

I view imbedded in a Navigation Controller which is slightly transparent. This view contains a UIscrollview. This UIScrollview has the following subviews: UIImageView, UILabel, and UIWebview. All these other views are working correctly. When the view loads, everything is in the correct position (The views are below the Navigation Bar). However, as soon as the screen is tapped, the scrollview repositions itself to the top of the view (aka behind the Navigation Bar, which is at the top, and I can see that this is true because the Nav Bar is slightly transparent)
Any idea why this is happening? I set up the scrollview like this:
self.scrollView.frame = CGRectMake(0, self.navigationController.navigationBar.frame.size.height, self.view.window.frame.size.width, self.view.window.frame.size.height);
Also set these properties:
self.scrollView.scrollEnabled = YES;
self.scrollView.alwaysBounceVertical = YES;
self.scrollView.bounces = YES;
Anybody know why this is happening?
I'd similar problem, but only on iOS 7.0. Try to set the translucent = NO property of the navigationBar. This will make the navigationBar opaque. Although if you want the navigationBar to be transparent, try to change the contentInset of the scrollView to adjust the content as you want. Hope that this will help :)

nib file custom view added programmatically is semi-transparent

I have a storyboard. I loaded a custom UIView from nib file to one of my UIViewController programatically.
Opaque and alpha values are checked and 1 respectively.
Even though the UIView is semi-transparent.
What would be the problem here?
A subview's maximum alpha value is the alpha value of its parent view.
The alpha of the child view is effectively the product of the two alpha values. A child with alpha of 1 on a parent with alpha of 0.5 will effectively have a 0.5 alpha.
If you need to put an opaque subview on a translucent or transparent parent view, there are two approaches.
The first approach is to move the would-be parent view to a subview as well so that both views are subviews of the same parent view. That parent view is just a clear-background view that acts as a container but has an alpha of 1.
The second approach is to make the parent view transparent or translucent while leaving its alpha at 1. This is done by setting its colors to have non-1 alpha values. For example:
[UIColor colorWithRed:1 green:0 blue:0 alpha: 0.3];
Rather than just:
someView.alpha = 0.3;

Screen bounds extends visible area

I'm trying to make a square (50 x 50) UIView in the lower right hand of my main view, but I'm confused about why it's not entirely visible. In this instance, only the tip of it is visible from the bottom. Am I confusing some concept?
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
UIView *transparentFloater = [[UIView alloc] initWithFrame:CGRectMake(width - 50.f, height - 50.0f, 50.0f, 50.0f)];
[self.view addSubview:transparentFloater];
Try to add the autoresizing mask:
transparentFloater.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
If I am not wrong, You have created your ViewController with xib and in the attributes of view, you have forgot to mention that you are using a "navigation bar" as "top bar".
Reason
So in ViewDidLoad, view in .xib with a height X is returned. But as you might have loaded the viewController using UINavigationController, after loading the height of view is decreased by 44pixels.
Suggestions :
1) Whenever you know that you are going to have navigation bar on top please mention it in .xib file as well.
For doing so click on the view in xib and select the option called topbar in attributes Inspector of utilities area
2) Call the following api
NSLog(#"In ViewDidAppear %#",NSStringFromCGRect(self.view.bounds));
in
viewDidLoad
viewDidAppear
to track whats happening with the bounds of view
You does not need to do anything just put
transparentFloater.backgroundColor = [UIColor redColor];
because your view (custom) is invisible (because your main view color and custom view color are same (white) ) may be. I tried your code in my demo project. it worked perfectly for me.

How do I get a UINavigationController to NOT change its view size when setting the translucent property?

I have an app where up until now I've been using a UINavigationController with a UINavigationBar that has its property translucent = YES. This means the UINavigationController's content view (i.e. the views from the view controllers you push) to be full-screen (minus status bar).
However, if you set the navigationBar.translucent = NO, this container view becomes 44pt shorter, as I suppose Apple has assumed you don't need any content under an opaque navigationBar.
... except if you're doing what we're doing and are employing a navigationBar that scrolls away (see This Post on how to do that) So I'd like to know if this is possible.
I want to have translucent = NO, but have everything behave as if it were still set to YES. I like the functionality of the translucent = YES, but I don't actually want the bar to be made translucent by UIKit.
What worked for me was to add
extendedLayoutIncludesOpaqueBars = true
in
viewDidLoad
something like this
override func viewDidLoad() {
super.viewDidLoad()
extendedLayoutIncludesOpaqueBars = true
}
Hope it will work for you as well
It's not necessarily a good answer but you could just offset your view that high if you're not translucent.
//This won't take into account orientation and probably other details
if(!self.navigationController.navigationBar.isTranslucent)
{
self.view.frame = CGRectMake(0,0,-44,self.view.bounds.size.height);
}
You could put that in your viewDidLoad or viewWillAppear and if you have a bunch of view controllers you can just subclass them all and put your logic in the subclass.
I found a solution that works, although it is indeed a bit of a hack.
The idea is to give the translucent nav bar an opaque backing. Unfortunately I'm not happy with the solution in that it's dirty and not encapsulated and introduces some potential issues, but i AM happy because it got the job done.
In my Application's base view controller class (i.e. MyViewController : UIViewController), in the viewDidLoad method, I instantiate a new ivar UIView *_navigationBarBG and give it the same frame as self.navigationController.navigationBar. I then set it's backgroundColor property to [UIColor whiteColor] although this is how you achieve some more tint I guess. [EDIT:If you wanted to be a purist (color values remaining exactly as they come from the .psd), you could make the _navigationBarBG a UIImageView and use your custom background there, and the background of the actual UINavigationBar you set to draw clear (or stretch a 1px transparent image if you wanted to use a typical 'change your navigation bar using an image' recipe that's somewhere on the internet)]
if(self.navigationController)
{
_navigationBarBG = [[UIView alloc] initWithFrame: self.navigationController.navigationBar.frame];
_navigationBarBG.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_navigationBarBG];
}
THEN, (and this is the crappy part, but I don't see any other way), I add this view as a subview. BUT, whenever you would normally make a call to [self.view addSubview: anyView], you have to make sure you call [self.view insertSubview: anyView belowSubview: _navigationBarBG];
if (_navigationBarBG)
[self.view insertSubview: anyView belowSubview:_navigationBarBG];
else
[self.view addSubview: anyView];
If you forget that, these added views will slide under your navbar background and look weird. So you need to know that this is a source of error.
WHY AM I DOING THIS? Again you might ask... I want to be able to have a scrolling navigation bar that scrolls out of the way when you scroll down your table view, thereby giving the user more screen space. This is done by using the scrollView delegate (scrollViewDidScroll:) and also viewWillAppear:
// FIRST DEAL WITH SCROLLING NAVIGATION BAR
CALayer *layer = self.navigationController.navigationBar.layer;
CGFloat contentOffsetY = scrollView.contentOffset.y;
CGPoint newPosition;
if (contentOffsetY > _scrollViewContentOffsetYThreshold && self.scrollingNavigationBarEnabled) {
newPosition = CGPointMake(layer.position.x,
22 - MIN((contentOffsetY - _scrollViewContentOffsetYThreshold), 48.0)); // my nav bar BG image is 48.0 tall
layer.position = newPosition;
[_navigationBarBG setCenter: newPosition]; // if it's nil, nothing happens
}
else
{
newPosition = kNavBarDefaultPosition; // i.e. CGPointMake(160, 22) -- portrait only
layer.position = newPosition;
[_navigationBarBG setCenter: newPosition]; // if it's nil, nothing happens
}
I was looking for an answer to this as I wanted my subviews to be at (0,0) and not (0,44)(in reference to the Screen bounds), but I could not find an answer on how to set this in the NavigationController, which I thought would be an included property.
What I ended up doing that was very simple is adding a subview to the navigation controller that was the width and height of the Navigation Bar, but then insert the subview below the Navigation Bar.
Now the setting is Translucent = YES, but it still appears solid and the subviews behave how I want.
EDIT: After re-reading your original post, I suppose if you're going to be rolling the nav bar away, you'll have to take into account hiding and showing the new subview as you do the same with the nav bar

Resources