有没有办法以编程方式选择segue锚点? [英] Is there a way to select a segue anchor programmatically?

查看:111
本文介绍了有没有办法以编程方式选择segue锚点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含一个包含按钮的视图的Storyboard。当用户按下此按钮时,会出现一个弹出窗口。

Suppose I have a Storyboard containing a view that contains a button. When the user presses this button, a popover comes up.

因此,我需要通过使用Xcode将segue拖动到按钮来设置锚点(然后执行 performSegueWithIdentifier:)。

Thus, I need to set an anchor by dragging the segue to the button using Xcode (and then do performSegueWithIdentifier:).

所以,我的问题是:有没有办法以编程方式设置这个锚?

So, my question is: is there a way to set this "anchor" programmatically?

谢谢。

推荐答案

在我的情况下,我已经以编程方式添加了几个UIBarButtonItem。
仅使用不可见视图作为存档的问题是,如果在我的情况下,UIBarButtonItem的大小正在改变它的大小,则弹出窗口的箭头不会出现居中,并且尽管它有效,但看起来像有点奇怪。

In my case I've added programmatically several UIBarButtonItem. The problem of only using an invisible view as an archor is that, if like in my case, the size of the UIBarButtonItem is changing it's size, the arrow of the popover doesnt appear centered, and althought it works, looks a bit strange.

如何解决它。

在故事板中创建一个小视图(大小并不重要),使其隐形,并链接它。
在我的情况下,这称为invisibleViewAsArchor

Create a small view in storyboard ( the size doesnt really matter ), make it invisible, and link it. In my case this is called invisibleViewAsArchor

使用跟随操作连接UIBarbutton项目。

Connect the UIBarbutton item with the follow action.

-(IBAction) showMyPopover:(id)sender {
    if([self.popoverController isPopoverVisible])
    {       
        [self.popoverController dismissPopoverAnimated:YES];
    }else{
        self.invisibleViewAsArchor.frame = CGRectMake([sender view].frame.origin.x,
                                                          [sender view].frame.origin.y-50,
                                                          [sender view].frame.size.width,
                                                          [sender view].frame.size.height);

        [self performSegueWithIdentifier:@"segue_to_something" sender:self];
    }
}

正如你在它显示弹出窗口之前所看到的那样performSegueWithIdentifier),我正在使用触发事件的按钮中的值更改Archor的帧

as you can see before it shows the popover (with performSegueWithIdentifier), I'm changing the frame of the Archor with the values from the button that has fired the event.

希望它有所帮助。

这篇关于有没有办法以编程方式选择segue锚点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆