代码之家  ›  专栏  ›  技术社区  ›  coopwatts

UIAlertController子视图中的星级控制在居中时被剪裁-Swift

  •  2
  • coopwatts  · 技术社区  · 9 年前

    我正在使用 Cosmos Star Rating Control 我将其嵌入为UIAlertController的子视图。我努力让它看起来正确,但我还是设法让它看起来像我想要的那样居中,但现在我相信星际控制的视图正在被剪掉,因为它不允许在某些恒星上选择某些区域,也不允许在打开时 clipToBounds = true 你可以明目张胆地看到它被剪掉了。我只是不确定为什么或如何解决这个问题。代码和一些屏幕截图如下。我还尝试使用自定义警报控制器,允许您添加自定义内容视图,但仍然遇到了相同的问题。

    @IBAction func rateButtonClicked(sender: AnyObject?) {
    
        //Alert for the rating
        let alert = UIAlertController(title: "\n\n", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)
    
        let customView = UIView(frame: CGRect(x: 0, y: 0, width: alert.view.frame.width, height: alert.view.frame.height))
    
        //The x/y coordinate of the rating view
        let xCoord = alert.view.frame.width/2 - 95 //(5 starts multiplied by 30 each, plus a 5 margin each / 2)
        let yCoord = CGFloat(25.0)
    
        ratingView.rating = 0.0
        ratingView.settings.starSize = 30
        ratingView.settings.emptyBorderColor = UIColor.blackColor()
        ratingView.settings.updateOnTouch = true
        ratingView.frame.origin.x = xCoord
        ratingView.frame.origin.y = yCoord
    
        customView.addSubview(ratingView)
    
        alert.addAction(UIAlertAction(title: "Save Rating", style: UIAlertActionStyle.Default, handler: ratingCompletionHandler))
    
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Destructive, handler: nil))
        alert.view.addSubview(customView)
    
        dispatch_async(dispatch_get_main_queue(), {
            self.presentViewController(alert, animated: true, completion: nil)
        })
    }
    

    启用clipToBounds的图像:

    enter image description here

    关闭clipToBounds的图像:

    enter image description here

    3 回复  |  直到 9 年前
        1
  •  2
  •   coopwatts    9 年前

    经过一番尝试后,我注意到CosmosView使用的是固有的内容大小,只会与它所容纳的内容(星星)一样大,我认为这是导致问题的原因,所以我使用了一个丑陋的方法,目前有效,只是给它分配了一个自定义大小的新框架,如下所示:

    //Make a custom frame
    ratingView.frame = CGRectMake(0, 0, 200.0, 60.0)
    ratingView.frame.origin.x = xCoord
    ratingView.frame.origin.y = yCoord
    

    这仍然以上面的星星为中心,但现在它们都可以点击,点击时提供正确的评分值。

        2
  •  0
  •   Nishant    9 年前

    尝试以下简单的方法可能会对您有所帮助:

    let customView = UIView(frame: CGRect(x: 0, y: 0, width: alert.view.frame.width, height: alert.view.frame.height))
    
    //add this line here
    customView.sizeToFit()
    
    //The x/y coordinate of the rating view
    let xCoord = customView.view.frame.width/2 - 95 //(5 starts multiplied by 30 each, plus a 5 margin each / 2)
    let yCoord = CGFloat(25.0)
    
        3
  •  0
  •   iAmcR    7 年前

    如果 preferredStyle为.alert 而不是.actionSheet。基于在显示警报控制器(视图)之前我们无法合法地知道大小的事实,我选择这样做(在Swift 3中):

    DispatchQueue.main.async {
                self.present(alert, animated: true, completion: {
                    if let sv = alert.view.subviews[0] {
                        let w = ratingView.intrinsicContentSize.width
                        let h = ratingView.intrinsicContentSize.height
                        ratingView.frame = CGRect(x: sv.bounds.width/2 - w/2, y: sv.bounds.height/2 - h/2, width: w, height: h)
                        sv.addSubview(ratingView)
                    }
                })
            } 
    

    注意:可能需要对.actionSheet进行调整。