菜鸟教程小白 发表于 2022-12-13 14:02:50

IOS设置UIViewController.view.frame = CGCGRect(x : -200. .) 不能点击按钮


                                            <p><p>我想把几个 UIViewController 放在一起:</p>

<pre><code>leftViewController.view.frame = CGRect(x: -200, y: 0.0, width: size.width/drawerSize, height: size.height)

    // Center Drawer
    centerViewController.view.frame = CGRect(x: leftViewController.view.frame.width, y: 0.0, width: centerWidth, height: size.height)

    // Right Drawer
    rightViewController.view.frame = CGRect(x: centerViewController.view.frame.origin.x + centerViewController.view.frame.size.width, y: 0.0, width: size.width/drawerSize, height: size.height)
</code></pre>

<p>在<strong>第一行</strong>我使用</p>

<pre><code>leftViewController.view.frame = CGRect(x: -200, y....)
</code></pre>

<p>它们会正确显示,但我无法单击 leftViewController 上的按钮。</p>

<p>如果</p>

<pre><code>leftViewController.view.frame = CGRect(x: 0.0, y...)
</code></pre>

<p>然后可以单击按钮,但这种布局不是我想要的。</p>

<p>完整代码由@Kevin Scardina <a href="https://stackoverflow.com/questions/24123004/slide-sidebar-menu-ios-8-swift" rel="noreferrer noopener nofollow">Slide Sidebar Menu IOS 8 Swift</a> 发布</p>

<p>它现在的功能如下图所示,我正在尝试将其修改为可以隐藏左右菜单的滑动菜单栏。</p>

<pre><code>/*
To use simply instantiate NVMDrawerController as your root view in your AppDelegate, or in the
   StoryBoard.
Once NVMDrawerController is instantiated, set the drawerSize of the NVMDrawerController,
and its leftViewControllerIdentifier, centerViewControllerIdentifier, and
   rightViewControllerIdentifier to the Storyboard Identifier of the UIViewController
you want in the different locations.
*/
class NVMDrawerController: UIViewController {

// This is where you set the drawer size (i.e. for 1/3rd use 3.0, for 1/5 use 5.0)
var drawerSize:CGFloat = 4.0
var leftViewControllerIdentifier:String = &#34;LeftController&#34;
var centerViewControllerIdentifier:String = &#34;CenterController&#34;
var rightViewControllerIdentifier:String = &#34;RightController&#34;

private var _leftViewController:UIViewController?
var leftViewController:UIViewController {
    get{
      if let vc = _leftViewController {
            return vc;
      }
      return UIViewController();
    }
}
private var _centerViewController:UIViewController?
var centerViewController:UIViewController {
    get{
      if let vc = _centerViewController {
            return vc;
      }
      return UIViewController();
    }
}
private var _rightViewController:UIViewController?
var rightViewController:UIViewController {
    get{
      if let vc = _rightViewController {
            return vc;
      }
      return UIViewController();
    }
}

static let NVMDrawerOpenLeft = 0
static let NVMDrawerOpenRight = 1
var openSide:Int {
    get{
      return _openSide;
    }
}
private var _openSide:Int = NVMDrawerOpenLeft

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    // Instantiate VC&#39;s with storyboard ID&#39;s
    _leftViewController = instantiateViewControllers(leftViewControllerIdentifier)
    _centerViewController = instantiateViewControllers(centerViewControllerIdentifier)
    _rightViewController = instantiateViewControllers(rightViewControllerIdentifier)

    // Call configDrawers() and pass the drawerSize variable.
    drawDrawers(UIScreen.mainScreen().bounds.size)

    self.view.addSubview(leftViewController.view)
    self.view.addSubview(centerViewController.view)
    self.view.addSubview(rightViewController.view)

}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -&gt; Void in
      // This is for beginning of transition
            self.drawDrawers(size)
      }, completion: { (UIViewControllerTransitionCoordinatorContext) -&gt; Void in
            // This is for after transition has completed.
      })

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Drawing View

func drawDrawers(size:CGSize) {
    // Calculate Center View&#39;s Size
    let centerWidth = (size.width/drawerSize) * (drawerSize - 1)

    // Left Drawer
    leftViewController.view.frame = CGRect(x: 0.0, y: 0.0, width: size.width/drawerSize, height: size.height)

    // Center Drawer
    centerViewController.view.frame = CGRect(x: leftViewController.view.frame.width, y: 0.0, width: centerWidth, height: size.height)

    // Right Drawer
    rightViewController.view.frame = CGRect(x: centerViewController.view.frame.origin.x + centerViewController.view.frame.size.width, y: 0.0, width: size.width/drawerSize, height: size.height)
    //rightViewController = rc

    // Capture the Swipes
    let swipeRight = UISwipeGestureRecognizer(target: self, action: Selector(&#34;swipeRightAction:&#34;))
    swipeRight.direction = .Right
    centerViewController.view.addGestureRecognizer(swipeRight)

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: Selector(&#34;swipeLeftAction:&#34;))
    swipeLeft.direction = .Left
    centerViewController.view.addGestureRecognizer(swipeLeft)

    if(openSide == NVMDrawerController.NVMDrawerOpenLeft){
      openLeftDrawer()
    }
    else{
      openRightDrawer()
    }
}

// MARK: - Open Drawers

func openLeftDrawer() {
    _openSide = NVMDrawerController.NVMDrawerOpenLeft
    UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseIn, animations:
      { () -&gt; Void in
            // move views here
            self.view.frame = CGRect(x: 0.0, y: 0.0, width: self.view.bounds.width, height: self.view.bounds.height)
      }, completion:
      { finished in
    })
}

func openRightDrawer() {
    _openSide = NVMDrawerController.NVMDrawerOpenRight
    UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseIn, animations:
      { () -&gt; Void in
            // move views here
            self.view.frame = CGRect(x: self.view.bounds.origin.x - self.leftViewController.view.bounds.size.width, y: 0.0, width: self.view.bounds.width, height: self.view.bounds.height)
      }, completion:
      { finished in
    })
}

// MARK: - Swipe Handling

func swipeRightAction(rec: UISwipeGestureRecognizer){
    self.openLeftDrawer()
}

func swipeLeftAction(rec:UISwipeGestureRecognizer){
    self.openRightDrawer()
}

// MARK: - Helpers

func instantiateViewControllers(storyboardID: String) -&gt; UIViewController {
    if let viewController = UIStoryboard(name: &#34;Main&#34;, bundle: nil).instantiateViewControllerWithIdentifier(&#34;\(storyboardID)&#34;) as? UIViewController{
      return viewController;
    }

    return UIViewController();
}
</code></pre>

<p>}</p>

<p> <a href="/image/9CH5I.png" rel="noreferrer noopener nofollow"><img src="/image/9CH5I.png" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>当您的 View 在其父 View 之外时,它无法接收任何触摸事件。您应该在 UIView(其父 View ) touchWithEvents 函数中枚举 subview 并使其接收事件。</p></p>
                                   
                                                <p style="font-size: 20px;">关于IOS设置UIViewController.view.frame = CGCGRect(x : -200. .) 不能点击按钮,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/35100177/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/35100177/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: IOS设置UIViewController.view.frame = CGCGRect(x : -200. .) 不能点击按钮