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

uiAlertViewConroller内的uiExtView约束

  •  0
  • MinnuKaAnae  · 技术社区  · 8 年前

    我有一个 uiExtView inside uiAlertViewController 。我需要将前导和尾随约束设置为 uitextview programmatically.

    屏幕截图

    这是我的密码

    func弹出控制器()
    {
    let alert=uiAlertController(标题:“附加说明”,消息:“在此处编写附加说明。”,首选样式:.alert)
    
    让textView=uitextView())
    textView.autoResizingMask=[.flexiblewidth,.flexibleheight]
    
    设为color=ui颜色(红色:224/255,绿色:224/255,蓝色:224/255,alpha:1.0)。cgcolor
    
    textView.layer.borderColor=颜色
    textView.layer.borderWidth=1.0
    textview.layer.cornerradius=5.0
    
    让controller=myActivitiesViewController()。
    
    textView.frame=控制器.view.frame
    controller.view.addSubView(文本视图)
    
    alert.setvalue(控制器,forkey:“contentviewcontroller”)。
    
    让height:nslayoutConstraint=nslayoutConstraint(项:alert.view,属性:.height,relatedby:.equal,toitem:nil,属性:.notanattribute,乘数:1,常量:180)
    alert.view.addConstraint(高度)
    
    let saveaction=uiAlertAction(标题:“OK”,样式:.default,处理程序:nil)
    let cancelaction=uiAlertAction(标题:“cancel”,样式:.cancel,处理程序:nil)
    
    alert.addaction(保存操作)
    alert.addaction(取消操作)
    
    
    自我呈现(警报,动画:真,完成:{
    
    textView.becomeFirstResponder()。
    })
    
    }
    < /代码> <静态地

    截图

    enter image description here

    这是我的密码

    func popUpController()
    {
        let alert = UIAlertController(title: "Additional Notes", message: "Write your additional notes here.", preferredStyle: .alert)
    
        let textView = UITextView()
        textView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    
        let color = UIColor(red: 224/255, green: 224/255, blue: 224/255, alpha: 1.0).cgColor
    
        textView.layer.borderColor = color
        textView.layer.borderWidth = 1.0
        textView.layer.cornerRadius = 5.0
    
        let controller = MyActivitiesViewController()
    
        textView.frame = controller.view.frame
        controller.view.addSubview(textView)
    
        alert.setValue(controller, forKey: "contentViewController")
    
        let height: NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 180)
        alert.view.addConstraint(height)
    
        let saveAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    
        alert.addAction(saveAction)
        alert.addAction(cancelAction)
    
    
        self.present(alert, animated: true, completion: {
    
            textView.becomeFirstResponder()
        })
    
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Anuraj    8 年前

    请注意,这只是一个工作。

    如果要为AlertController提供更高的高度,请添加更多 新行 >至 title >。

    如果您希望修改此项,请根据您的要求调整约束

    func showalertcontroller())
    {
    
    let alertController=uiAlertController(标题:“您的标题\n\n\n\n\n\n”,消息:nil,首选样式:uiAlertControllerStyle.alert)
    让customView=uitextView()。
    customView.translatesAutoResizingMaskinToConstraints=false
    alertController.view.autoresizeSubviews=true
    让someThingAction=uiAlertAction(标题:“some action”,样式:uiAlertActionStyle.default,处理程序:(alert:uiAlertAction!)在里面
    
    打印(自定义视图.text)
    
    })
    
    Let CancelAction=uiAlertAction(标题:“Cancel”,样式:uiAlertActionStyle.Cancel,处理程序:(警报:uiAlertAction!)打印(“取消”))
    
    AlertController.AddAction(someThingAction)
    alertController.addAction(取消操作)
    
    
    alertController.view.addSubView(自定义视图)
    
    customView.topAnchor.constraint(等于:alertController.view.topAnchor,常量:50)。isActive=true
    
    customView.rightAnchor.constraint(等于:alertController.view.rightAnchor,常量:-10)。isActive=true
    customView.leftAnchor.constraint(等于:alertController.view.leftAnchor,常量:10)。isActive=true
    customView.bottomAnchor.constraint(等于:alertController.view.bottomAnchor,常量:-60)。isActive=true
    customView.backgroundcolor=uicolor.red
    self.present(警报控制器,动画:真){
    
    }
    
    }
    < /代码> 
    
    

    标题.

    调整约束条件根据您的要求,如果您想修改这个

     func showAlertController()
        {
    
            let alertController = UIAlertController(title: "Your title \n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.alert)
            let customView = UITextView()
            customView.translatesAutoresizingMaskIntoConstraints = false
            alertController.view.autoresizesSubviews = true
            let somethingAction = UIAlertAction(title: "Some Action", style: UIAlertActionStyle.default, handler: {(alert: UIAlertAction!) in
    
                print(customView.text)
    
            })
    
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: {(alert: UIAlertAction!) in print("cancel")})
    
            alertController.addAction(somethingAction)
            alertController.addAction(cancelAction)
    
    
            alertController.view.addSubview(customView)
    
            customView.topAnchor.constraint(equalTo: alertController.view.topAnchor, constant: 50).isActive = true
    
            customView.rightAnchor.constraint(equalTo: alertController.view.rightAnchor, constant: -10).isActive = true
            customView.leftAnchor.constraint(equalTo: alertController.view.leftAnchor, constant: 10).isActive = true
            customView.bottomAnchor.constraint(equalTo: alertController.view.bottomAnchor, constant: -60).isActive = true
            customView.backgroundColor = UIColor.red
            self.present(alertController, animated: true) {
    
            }
    
        }
    

    enter image description here