swift bringSubViewToFront - ios

I have a bottomView with opacity set to 0.65, and embedded in that View I have 5 buttons - which also gets the 0.65 opacity attribute - but How do I make the buttons get rid of the opacity?
I want the buttons to be very clear
I have tried to make outlets of the View and the Buttons and set the buttons to the front - view, but it doesn't change the appearance of the button??
#IBOutlet weak var bottomView: UIView!
#IBOutlet weak var findVejOutlet: UIButton!
#IBOutlet var superViewOutlet: UIView!
#IBAction func findVejButton(_ sender: Any) {
superViewOutlet.bringSubview(toFront: findVejOutlet)
}
override func viewDidLoad() {
super.viewDidLoad()
settingView()
}
func settingView(){
bottomView.bringSubview(toFront: findVejOutlet)
}

If you set the opacity of a view to a value of less than 1, it makes all the contents of a view partly transparent (including subviews). You can't change that, and opacity has nothing to do with the front-to-back ordering of the views.
You either need to make the parent view fully opaque and the non-button subviews partly transparent, or remove the buttons from the translucent view and instead put them in the common parent view.

Related

View Controller loses connection to constraints in IBOutlets when switching tabs in UITabView

OK, so here's the relevant bit of my code:
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var questionView: QuestionView!
#IBOutlet weak var answerView: AnswerView!
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var tableViewTopConstraint: NSLayoutConstraint! // Active at start, attaches tableView.topAnchor to questionView.bottomAnchor
#IBOutlet weak var tableViewTopAlternateConstraint: NSLayoutConstraint! // Inactive at start, attaches tableView.topAnchor to answerView.bottomAnchor
func submitAnswer() {
tableViewTopConstraint.isActive = false
tableViewTopAlternateConstraint = true
}
func newQuestion() {
tableViewTopConstraint.isActive = true
tableViewTopAlternateConstraint = false
}
}
I'm building a question/answer type of app and the two subviews are different sizes for design reasons. I have it set so that it toggles between the two subviews depending on the current state (toggling back for a new question when the user taps to advance), and it works fine...
...until I go to a different tab on the UITabView that encloses everything (e.g. to change the settings then resume testing)
The moment the tab changes it's like the NSLayoutConstraint outlets no longer exist. The orders to change them still process (I've verified this in the console), but they do nothing.
I've tried declaring the outlets as strong (there was no difference in behavior)
I've removed the outlets and managed them in code (it worked the first time, but afterward it stretched the shorter QuestionView's height to match tableView's new top position instead of moving tableView up to the bottom of QuestionView as was intended)
I've tried implementing a UITabViewController so that I could call tabBar(_:didSelect:) and replace the view controller with a brand new instance of MyViewController() whenever I switch back to that tab (It loads the first time, but when I try to switch tabs it finds nil when accessing any of the IBOutlets...even though I didn't tap on the tab for the testing view and filtered by item.tag when replacing the existing view controller)
Any suggestions on how else to attack this problem?
First, don't set #IBOutlet properties to weak. I know that's the default, but it's not correct.
If you have this:
#IBOutlet weak var tableViewTopAlternateConstraint: NSLayoutConstraint!
connected to a constraint via Storyboard, and you do this:
tableViewTopAlternateConstraint.isActive = false
you just removed that constraint from existence.
In addition, if you have two different Top constraints in Storyboard, you should be getting an Error indicator as they cannot be satisfied.
Better to either change the Priorities, or use a single constraint and change the .constant value.
So, two constraints in Storyboard:
set the "default" constraint to Priority: High (750)
set the "alternate" constraint to PriorityL Low (250)
and your code becomes:
func submitAnswer() {
tableViewTopConstraint.priority = .defaultLow
tableViewTopAlternateConstraint.priority = .defaultHigh
}
func newQuestion() {
tableViewTopAlternateConstraint.priority = .defaultLow
tableViewTopConstraint.priority = .defaultHigh
}

How to change total screen background colour except popup view in swift

I cant create saperate custom popview.. so I have tried like below mentioned one of the answer
here is storyboard view hierarchy
popBG view background colour is black with alpha = 0.3
this is code:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var popViewtop: UIView!
#IBOutlet weak var testTable: UITableView!
#IBOutlet weak var popView: UIView!
#IBOutlet weak var popBG: UIView!
override func viewDidLoad() {
super.viewDidLoad()
popViewtop.isHidden = true
}
#IBAction func testBtn(_ sender: Any) {
popViewtop.isHidden = false
}
#IBAction func btnPop(_ sender: Any) {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "NewZoomAddressViewController") as! NewZoomAddressViewController;
popViewtop.isHidden = true
self.navigationController?.pushViewController(viewController, animated: true);
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
}
now the tableview and button are not showing transperently.. only popbg and popview coming.. did i miss anything..
Actually i need like this: total backgroundview in some darkcolour and popupview heighlighted in white colour
if giving alpha value to view,
view's subView's alpha value changes, including popupView
if giving background color to view, including popupView
view's subView's alpha value does not change, including popupView
popupView is a subView of view,view's alpha value affects its subviews's alpha.
view.addSubview(popupView)
Your current structure:
RootView->Subviews //Changing RootView alpha effects Subviews.
The solution is that the popupView is not a subview of view , which you show color changing with
Need a container view to separate from popupView
// backgroundColorChangeContainerView add other views ( tableView ... )
view.addSubview(backgroundColorChangeContainerView)
view.addSubview(popupView)
Make your Popup view full screen ..which have background and have subview as popUp over that background ...so you dont need to change anything once you show or hide popup and its easy to implement... Full screen UIView having popUp UIView over it
You should have a view called it MainPopUpView ... in this view you will add two UIViews ...
Background View ... with black color with alpha 0.3
PopUpUI View ... which shows actual popup
-> backGround UIView full screen (with alpha 0.3)
MainPopUpView
-> popUpView short & centre and shows actual content
So MainPopUpView have two views ...
here is the hierarchy
other way to achieve like your final image.. custom pop controller avoids this situation
put your popupView in a custom viewController, then
#IBAction func addAddressBtn(_ sender: Any) {
present(PopupViewController(nibName: "PopupViewController", bundle: nil), animated: true) {}
}

InputAccessoryView with custom view created in IB

I've read a lot of material on this topic but most of them create custom view programatically.
Is it possible to use InputAccessoryView with a custom view created in IB? In storyboard I've added textInputView, inside which I've added text view and send button etc as seen in the screenshot below.
I've following code so which removes Table View for some reason so I can't get it working. I've added tableview in the storyboard.
I've shown here only InputAccessoryView related code.
class InputAccViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var textView: UITextView!
#IBOutlet weak var textInputView: UIView!
// other code
override var canBecomeFirstResponder: Bool { return true }
override var inputAccessoryView:UIView {
get{
return self.textInputView
}
}
override func viewDidLoad() {
super.viewDidLoad()
textInputView.removeFromSuperview()
// other code
}
// other code tableview delegates etc...
}
Left screenshot is with the accessory view code which doesn't show table view. If I comment out accessory view related code it does show table view as in the right screenshot.
Seems you might be constraining the UITableView's bottom to the textInputView's top. When you are setting the textInputView as the inputAccessoryView of the UIViewController this no longer works as expected. When setting the textInputView as the inputAccessoryView make sure you constraint the bottom of UITableView to the bottom of UIViewController's view.

View over tableView

I have the following screen (image) which shows a side menu when the button is pressed the menu appears, plus I have a uitableview to show other things
my problem is that when the side menu appears the uitableview is resized.
How do I keep my uitableview intact?
I've been trying the next but I have not got anything:
#IBOutlet weak var Vmenu_usuario: UIView!
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
view.sendSubview(toBack:tableView)
view.bringSubview(toFront:Vmenu_usuario)
}
If you want to delete White space view over tableview
uncheck the option 'Adjust Scroll View Insets'.
more detail: Empty white space above UITableView inside a UIView
Hope this hellp.

Toggle two views with same constraints, one is wrong aligned

I'm trying to toggle between 2 tableviews based on a segment control. To do this, I've tested 2 implementations:
Option A: Have 2 container views stacked in a storyboard (same constraints), then toggle the container views based on the segment control.
Option B: Have 1 container view setup, add and remove the views programmatically.
The problem with both implementations is that the first view to display is always aligned correctly (no matter if its view A or view B), the second one is wrong aligned! rrrr
Why is the second view to display wrong aligned!?!?
Here is the code for example A:
class MainViewController: UIViewController {
#IBOutlet weak var viewOne: UIView!
#IBOutlet weak var viewTwo: UIView!
#IBOutlet weak var segmentContrl: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
showView(0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func showView(index:Int){
switch index
{
case 0:
viewOne.hidden = false
viewTwo.hidden = true
break;
case 1:
viewOne.hidden = true
viewTwo.hidden = false
break;
default:
break;
}
}
#IBAction func onSegmentChanged(sender: AnyObject) {
print("onSegmentChanged \( segmentContrl.selectedSegmentIndex)")
showView(segmentContrl.selectedSegmentIndex);
}
Storyboard, the container views are stacked:
The container views constraints, both have the same!
Fixed the problem by removing "Adjust scroll view insets" in all 3 view controllers shown above.
Then I repositioned the container views to top and bottom align then to the layout guides.
WORKS! :P

Resources