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

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) {}
}

Related

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.

swift bringSubViewToFront

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.

Reset subviews in segmented control

I am using a segmented control to rotate through content, but when I scroll to the end of a subview and switch to another segment, the new segment shows roughly in the same position as the previous segment (i.e. if I scroll to the bottom of segment A and switch segments, segment B is also at the bottom of the scroll). How can I reset each segment so that it is positioned at the top each time I change segments?
Sample code:
class AboutUsViewController: UIViewController {
#IBOutlet weak var welcomeContainer: UIView!
var views: [UIView]!
override func viewDidLoad() {
super.viewDidLoad()
views = [UIView]()
views.append(ViewController1().view)
views.append(ViewController2().view)
views.append(ViewController3().view)
views.append(ViewController4().view)
for view in views {
welcomeContainer.addSubview(view)
}
welcomeContainer.bringSubview(toFront: views[0])
}
#IBAction func swichViewAction(_ sender: UISegmentedControl) {
self.welcomeContainer.bringSubview(toFront: views[sender.selectedSegmentIndex])
}
}

Elements in View Controller Disappear When Clicking or Tapping Them

I am developing an app on XCode using Swift 2 and have run into an error with my UI. I have placed a UILabel, UITextView, and UITextField on the View Controller of interest.
Before inputting the UITextField, everything worked fine. I inserted the text field and assigned the proper constraints and when simulating the app, clicking on the text field will cause all of the UI elements to disappear or the alpha to go instantaneously to zero (I'm not sure which of the two is actually happening).
I'm not receiving any feedback via the console and the app itself does not crash.
Here is my code:
//Declare the following UI objects to be manipulated
#IBOutlet weak var labelOneTitle: UILabel!
#IBOutlet weak var textDescription: UITextView!
#IBOutlet weak var fieldInput: UITextField!
override func viewDidLayoutSubviews() {
//Set UI object alphas to zero prior to loading the view
labelOneTitle.alpha = 0
textDescription.alpha = 0
fieldInput.alpha = 0
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
UIView.animateWithDuration(2) { () -> Void in
self.labelOneTitle.alpha = 1
}
UIView.animateWithDuration(3) { () -> Void in
self.textDescription.alpha = 1
}
UIView.animateWithDuration(3) { () -> Void in
self.fieldInput.alpha = 1
}
}
It might be the UIViewController.viewDidLayoutSubviews rechecking all the constraints and reconfiguring the view as per the the class reference:
When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout.
Your view controller can override this method to make changes after
the view lays out its subviews. The default implementation of this
method does nothing.
When you click on the UITextField the keyboard appears (Probably) and thus recalling the method to set the alpha of all your UI Elements to 0.
If you'd move setting the alpha's to 0 to the viewDidLoad() than the problem might go away.

Using UIScrollView to cycle through images

I have an imageView inside of a scrollView, and I want to be able to upload up to 3 images and have the user swipe between them. I have accomplished this simply by replacing the contents of the view with the information I want, but it doesn't look or feel as good as a scroll view with pagination would.
I have all of the code to make each image appear when I want it, but my problem is with the scrollview constraints and how to add more content to a scrollview with fixed width. No matter what I do, I can't seem to extend the scroll range when I add an image to the view.
here is what I'm trying now:
let newImageView:UIImageView = UIImageView()
self.imageScrollView.contentSize = CGSize(width: self.imageView.frame.size.width * 3, height: self.imageView.frame.size.height)
self.imageScrollView.pagingEnabled = true
for i = 0; i < 3; ++i {
var xOrigin:CGFloat = CGFloat(i) * self.view.frame.size.width
newImageView.frame = CGRectMake(self.imageView.image!.size.width * i, 0, self.imageView.image!.size.width, self.imageScrollView.frame.size.height)
newImageView.image = images[i]
newImageView.contentMode = UIViewContentMode.ScaleAspectFit
self.imageScrollView.addSubview(newImageView)
}
With this code I am able to add the images, but I cannot scroll to them. In my storyboard, the scrollview constraints look like this:
I'm sure some of these constraints are redundant, but I can't figure out why the content width won't extend and allow me to scroll through all of the images in the scrollview.
EDIT
I have made changes by using the textbook examples from #matt. I made a new view controller and put it inside a view in my other view controller (the same one as I had originally). It displays the first one no problem, but it doesn't seem to call teh UIPageViewControllerDataSource functions when I swipe left or right. I'm not sure why this is the case.
Here's what it looks like in the original view controller (UploadPhotoViewController)
func setUpPageViewController() {
sharedIm.images.append(UIImage(named: "placeholder")!)
sharedIm.images.append(UIImage(named: "TestProfile")!)
sharedIm.images.append(UIImage(named: "button")!)
let pvc = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
let page:ImagePage = ImagePage()
pvc.dataSource = self
pvc.setViewControllers([page], direction: .Forward, animated: false, completion: nil)
pvc.view.frame = self.imageContainer.bounds
self.imageContainer.addSubview(pvc.view)
pvc.didMoveToParentViewController(self)
}
//this is placed at the end of the class
extension UploadPhotoViewController : UIPageViewControllerDataSource {
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let image = (viewController as! ImagePage).image
let ix = find(sharedIm.images, image)! + 1
println(ix)
if ix >= sharedIm.images.count {
println("no next page")
return nil
}
sharedIm.currentIndex = ix
let page = ImagePage()
return page
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let image = (viewController as! ImagePage).image
let ix = find(sharedIm.images, image)! - 1
println(ix)
if ix < 0 {
println("no previous page")
return nil
}
sharedIm.currentIndex = ix
let page = ImagePage()
return page
}
And here is what is in my new view controller that is being added to a view of the other one:
class ImagePage: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var image : UIImage?
var imagePicker: UIImagePickerController!
#IBOutlet var imageView : UIImageView!
#IBOutlet var forwardButton: UIButton!
#IBOutlet var backButton: UIButton!
#IBOutlet var closeButton: UIButton!
#IBOutlet var pictureButton: UIButton!
#IBOutlet var cameraButton: UIButton!
init() {
self.image = sharedIm.images[sharedIm.currentIndex]
super.init(nibName: "ImagePage", bundle: nil)
}
required init(coder: NSCoder) {
fatalError("NSCoding not supported")
}
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.image = self.image
}
//bunch of other code for buttons and uploading pictures
}
What you're trying to do would be a lot easier with a UIPageViewController (with a scrolling style). I would suggest that you switch to a UIPageViewController implementation instead.
The reason is that you only supply one "page" (in your case, an image) at a time. You can decide in real time whether there is another "page" in either direction, and how many "pages" you want to pretend there "really" are. The bookkeeping work happens in your model (the data behind the scenes), rather than your having to configure the entire view to handle all pages at once, as UIScrollView makes you do.
When working with autolayout and uiscrollview, the trick is to know when to load your content. Since you are working in a hybrid mode (frames and autolayout) you need to make sure the constraints have had time to load and then AFTER they have loaded, you can add your subviews and adjust the constraints. If you try to load your content on viewDidLoad, it will be too early and your constraints may not have been set yet. If you load your content on viewWillAppear, then it's too late and the constraints have already been locked in for presenting the view to the user.
So, I have found the best place to load content is on the viewDidLayoutSubviews At this point autolayout has applied the constraints to your scrollView and you are now safe to make changes. Add a call here to run your code that populates the scrollView and you should be fine. Also, you will need to set a flag to only make the call once as viewDidLayoutSubviews can get called several times during view creation. You should also call setNeedsUpdateConstraints on your scrollView to make sure its constraints are updated as you add your subviews.

Resources