如何在视图中心添加视图

时间:2017-07-27 11:01:17

标签: ios swift swift3

我有200x200宽度和高度的视图

<Window.Resources>
    <ResourceDictionary>
        <DataTemplate DataType="{x:Type testClean:TestViewModel}">
            <Grid>
                <Viewbox>
                    <TextBlock Foreground="HotPink">just some pink text</TextBlock>
                </Viewbox>
                <ComboBox Height="50" Width="200" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="20" SelectedIndex="0" 
                          ItemsSource="{Binding ItemsSource}"
                          SelectedItem="{Binding SelectedItem}"/>
                <Button Command="{Binding Command}" Height="50" Width="100" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="250,20,20,20">Reset</Button>
            </Grid>
        </DataTemplate>
    </ResourceDictionary>
</Window.Resources>

<ContentControl Content="{Binding}" />

我想要将pulsator设置为viewSearchArea的中心。 我怎么能这样做? 如果有人想知道这里的脉动是什么link

3 个答案:

答案 0 :(得分:2)

试试这个。

pulsator.position = CGPoint(viewSearchArea.layer.bounds.midX, viewSearchArea.layer.bounds.midY)

答案 1 :(得分:0)

像这样设置框架:

       let height:CGFloat = 200.0
        let width:CGFloat = 200.0
        let xPosition = (self.view.frame.size.width/2) - (width/2);
        let yPosition = (self.view.frame.size.height/2) - (height/2);
        let pulsator = UIView()
        pulsator.frame = CGRect.init(x:xPosition , y: yPosition, width: width, height: height)

设置中心的另一个选项:

pulsator.center = self.view.center;

答案 2 :(得分:0)

我在示例项目中找到了这个:

pulsator.position = viewSearchArea.layer.position

试试这个或那个:

pulsator.frame = CGRect(x: (viewSearchArea.frame.width/2) - 100, y: (viewSearchArea.frame.height/2) - 100, width: 200, height: 200)