Does anybody know is there any way to call status bar tapping programmatically when view appeared? My view is not on the top when appearing.
Thank you!
I'm going to assume that what you want to do is scroll your view to the top, which is what tapping the status bar does. If that's the case then here is a nice little view controller extension to achieve that.
extension UIViewController {
func scrollToTop() {
func scrollToTop(view: UIView?) {
guard let view = view else { return }
switch view {
case let scrollView as UIScrollView:
if scrollView.scrollsToTop == true {
scrollView.setContentOffset(CGPoint(x: 0.0, y: -scrollView.contentInset.top), animated: true)
return
}
default:
break
}
for subView in view.subviews {
scrollToTop(view: subView)
}
}
scrollToTop(view: view)
}
var isScrolledToTop: Bool {
for subView in view.subviews {
if let scrollView = subView as? UIScrollView {
return (scrollView.contentOffset.y == 0)
}
}
return true
}
}
Then in the viewDidAppear of your view controller, you can call
myViewController.scrollToTop()
Related
func clearBackgroundColor(of view: UIView) {
if let effectsView = view as? UIVisualEffectView {
effectsView.removeFromSuperview()
return
}
view.backgroundColor = .clear
view.subviews.forEach { (subview) in
clearBackground(of: subview)
}
}
I have already tried this code and the resulting AlertController's border has removed but the border of buttons inside AlertController is still there.
I'm trying to get the content size of UITableView in runtime, where I working on library that will do some moves when pop over the views and to do this I wrote this code inside the library to figure out that inside the presented view will be UITableView:
if view.isKind(of: UIView.self) {
for subView in view.subviews {
for sv in subView.subviews {
if sv.isKind(of: UITableView.self) {
print(sv.frame.size.height)
Here I got the table view, but I couldn't get the contentSize property
print("table view found")
}
}
}
}
Any tips how to get its content size ?!
Use a cast instead of a test so that the compiler knows the object's properties:
if view.isKind(of: UIView.self) {
for subView in view.subviews {
for sv in subView.subviews {
if let tableView = sv as? UITableView {
print(tableView.contentSize.height)
}
}
}
}
I have a UITabBarController and I added a view controller. In my UIViewController I have a UITableView.
My requirement is whenever I will scroll down tableView then Tab bar should be hidden and TableView should come at full screen and when I scroll tableView up then TabBar should not be hidden.
Here is my story-board screen shots:
In Images you can see I have taken tableView and tabBar. And also TableView height I have to show full when TabBar will be hidden.
Here is my code:
//Change Tab bar
func changeTabBar(hidden:Bool, animated: Bool){
let tabBar = self.tabBarController?.tabBar
if tabBar!.isHidden == hidden{ return }
let frame = tabBar?.frame
let offset = (hidden ? (frame?.size.height)! : -(frame?.size.height)!)
let duration:TimeInterval = (animated ? 0.5 : 0.0)
tabBar?.isHidden = false
if frame != nil
{
UIView.animate(withDuration: duration,
animations: {
tabBar!.frame = frame!.offsetBy(dx: 0, dy: offset)
//tabBar?.isHidden = true
//self.tableView.frame.size.height = self.tableView.frame.size.height + (tabBar?.frame.size.height)!
},
completion: {
if $0 {tabBar?.isHidden = hidden}
})
}
}
//ScrollView Delegate
extension reNewView : UIScrollViewDelegate {
//Tab bar
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
changeTabBar(hidden: true, animated: true)
}
else{
changeTabBar(hidden: false, animated: true)
}
}
}
What to do ?
I checked this link also but still not getting proper answer.
I have UIButton inside cell of UITableView, When i touch the button it is actually working but the animation of highlighted is not being seen, I have tried setting delaysContentTouches = false on viewDidLoad and also on IB
I even tried to find UIScrollVIew of table view so that i can set that properties to false, like...
for cls in cell.subviews
{
println("name of class is ::\(NSStringFromClass(cls.classForCoder))")
if NSStringFromClass(cls.classForCoder) == "UITableViewCellScrollView"
{
cls.scrollView.delaysContentTouches = false
break
}
}
Thanks in advance!
UITableView subclass
override public var delaysContentTouches: Bool {
didSet {
if let subviews = self.subviews as? [UIView] {
for view in subviews {
if let scroll = view as? UIScrollView {
scroll.delaysContentTouches = delaysContentTouches
}
break
}
}
}
}
I have a random child view in a view hierarchy. What is the best/fastest/cleverest way to get to the root superview?
Cheers,
Doug
If your app only has one UIWindow (usually true), and if that window's only subview is your root controller's view (also usually true):
[randomChildView.window.subviews objectAtIndex:0]
Or you could recursively climb out of the hierarchy, by checking view.superview until you find a view whose superview is nil.
It's an insignificant amount of processor time to go straight up the superview hierarchy. I have this in my UIView category.
- (UIView *)rootView {
UIView *view = self;
while (view.superview != Nil) {
view = view.superview;
}
return view;
}
swift3/4:
func rootSuperView() -> UIView
{
var view = self
while let s = view.superview {
view = s
}
return view
}
I like this one.
extension UIView {
func rootView() -> UIView {
return superview?.rootView() ?? superview ?? self
}
}
Fast solution (fast as in minimalist):
extension UIView {
var rootView: UIView {
superview?.rootView ?? self
}
}